Class: Hako::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/hako/cli.rb

Defined Under Namespace

Classes: Deploy, Oneshot, Remove, Rollback, ShowDefinition, Status, Stop

Constant Summary collapse

SUB_COMMANDS =
%w[
  deploy
  rollback
  oneshot
  show-definition
  status
  remove
  stop
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



24
25
26
27
28
# File 'lib/hako/cli.rb', line 24

def initialize(argv)
  @argv = argv.dup
  @help = false
  parser.order!(@argv)
end

Class Method Details

.start(argv) ⇒ Object



20
21
22
# File 'lib/hako/cli.rb', line 20

def self.start(argv)
  new(argv).run
end

Instance Method Details

#create_subcommand(sub) ⇒ Object (private)



51
52
53
54
55
56
57
58
# File 'lib/hako/cli.rb', line 51

def create_subcommand(sub)
  if SUB_COMMANDS.include?(sub)
    CLI.const_get(sub.split('-').map(&:capitalize).join(''))
  else
    $stderr.puts "No such subcommand: #{sub}"
    exit 1
  end
end

#parserObject (private)



43
44
45
46
47
48
49
# File 'lib/hako/cli.rb', line 43

def parser
  @parser ||= OptionParser.new do |opts|
    opts.banner = 'hako'
    opts.version = VERSION
    opts.on('-h', '--help', 'Show help') { @help = true }
  end
end

#runObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/hako/cli.rb', line 30

def run
  if @help || @argv.empty?
    puts parser.help
    SUB_COMMANDS.each do |subcommand|
      puts create_subcommand(subcommand).new.parser.help
    end
  else
    create_subcommand(@argv.shift).new.run(@argv)
  end
end