Class: Kontena::Plugin::Shell::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/plugin/shell/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil, args = nil, session = nil) ⇒ Command

Returns a new instance of Command.



44
45
46
47
48
# File 'lib/kontena/plugin/shell/command.rb', line 44

def initialize(context = nil, args = nil, session = nil)
  @args = Array(args)
  @context = context
  @session = session
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/kontena/plugin/shell/command.rb', line 5

def args
  @args
end

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/kontena/plugin/shell/command.rb', line 5

def context
  @context
end

#sessionObject (readonly)

Returns the value of attribute session.



5
6
7
# File 'lib/kontena/plugin/shell/command.rb', line 5

def session
  @session
end

Class Method Details

.command(name = nil) ⇒ Object



7
8
9
10
11
12
# File 'lib/kontena/plugin/shell/command.rb', line 7

def self.command(name = nil)
  return @command if instance_variable_defined?(:@command)
  disabled_commands = ENV['KOSH_DISABLED_COMMANDS'].to_s.split(/,/)
  Array(name).each { |name| Shell.commands[name] = self unless disabled_commands.include?(name) }
  @command = name
end

.completions(*completions) ⇒ Object



40
41
42
# File 'lib/kontena/plugin/shell/command.rb', line 40

def self.completions(*completions)
  @completions ||= completions
end

.description(text = nil) ⇒ Object



32
33
34
# File 'lib/kontena/plugin/shell/command.rb', line 32

def self.description(text = nil)
  @description ||= text
end

.has_subcommands?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/kontena/plugin/shell/command.rb', line 24

def self.has_subcommands?
  !subcommands.nil? && !subcommands.empty?
end

.help(text = nil) ⇒ Object



36
37
38
# File 'lib/kontena/plugin/shell/command.rb', line 36

def self.help(text = nil)
  @help ||= text
end

.subcommands(subcommands = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/kontena/plugin/shell/command.rb', line 14

def self.subcommands(subcommands = nil)
  return @subcommands if instance_variable_defined?(:@subcommands)
  @subcommands = {}
  Array(subcommands).each do |sc|
    Array(sc.command).each do |name|
      @subcommands[name] = sc
    end
  end
end

Instance Method Details

#has_subcommands?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/kontena/plugin/shell/command.rb', line 28

def has_subcommands?
  self.class.has_subcommands?
end

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kontena/plugin/shell/command.rb', line 50

def run
  if has_subcommands?
    if args[1]
      subcommand = self.class.subcommands[args[1]]
      if subcommand
        subcommand.new(context, args[1..-1], session).run
      else
        puts Kontena.pastel.red("Unknown subcommand")
      end
    else
      context << args[0]
    end
  else
    execute
  end
rescue Kontena::Plugin::Shell::ExitCommand::CleanExit
  puts Kontena.pastel.green("Bye!")
  exit 0
rescue => ex
  puts Kontena.pastel.red("ERROR: " + ex.message)
  puts Kontena.pastel.green(ex.backtrace.join("\n  ")) if ENV["DEBUG"]
end