Class: Kontena::Plugin::Shell::KontenaCommand

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

Instance Attribute Summary

Attributes inherited from Command

#args, #context, #session

Instance Method Summary collapse

Methods inherited from Command

command, completions, description, #has_subcommands?, has_subcommands?, help, #initialize, #run, subcommands

Constructor Details

This class inherits a constructor from Kontena::Plugin::Shell::Command

Instance Method Details

#cmdObject



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

def cmd
  cmd = Kontena::MainCommand.new('')
  cmdline = context.to_a + args
  cmdline.shift if cmdline.first == 'kontena'
  cmd.parse(cmdline)
  cmd
end

#executeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kontena/plugin/shell/commands/kontena.rb', line 23

def execute
  if cmd.subcommand_name && cmd.subcommand_name == 'shell'
    puts Kontena.pastel.red("Already running inside KOSH")
  else
    cmd.run([])
  end
rescue Clamp::HelpWanted => ex
  if args.include?('--help') || args.include?('-h')
    puts subcommand_class.help('')
  else
    context.concat(args)
  end
rescue SystemExit => ex
  puts Kontena.pastel.red('[Command exited with error]') unless ex.status.zero?
rescue => ex
  puts Kontena.pastel.red("ERROR: #{ex.message}")
ensure
  Thread.main['spinners'] && Thread.main['spinners'].map(&:kill) && Thread.main['spinners'] = nil
end

#subcommand_classObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/kontena/plugin/shell/commands/kontena.rb', line 43

def subcommand_class
  (context + args).reject { |t| t.start_with?('-') }.inject(Kontena::MainCommand) do |base, token|
    if base.has_subcommands?
      sc = base.recognised_subcommands.find { |sc| sc.names.include?(token) }
      sc ? sc.subcommand_class : base
    else
      base
    end
  end
end