Class: Kaiser::Cli

Inherits:
Object
  • Object
show all
Extended by:
CliOptions
Defined in:
lib/kaiser/cli.rb

Overview

The commandline

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CliOptions

option, options

Class Method Details

.all_subcommands_usageObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kaiser/cli.rb', line 80

def self.all_subcommands_usage
  output = ''

  @subcommands.each do |name, klass|
    name_s = name.to_s

    output += name_s + "\n"
    output += name_s.gsub(/./, '-')
    output += "\n"
    output += klass.usage
    output += "\n\n"
  end

  output
end

.register(name, klass) ⇒ Object



43
44
45
46
# File 'lib/kaiser/cli.rb', line 43

def self.register(name, klass)
  @subcommands ||= {}
  @subcommands[name] = klass.new
end

.run_command(name, global_opts) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kaiser/cli.rb', line 48

def self.run_command(name, global_opts)
  cmd = @subcommands[name]
  opts = cmd.define_options(global_opts + cmd.class.options)

  # The define_options method has stripped all arguments from the cli so now
  # all that we're left with in ARGV are the subcommand to be run and possibly
  # its own subcommands. We remove the subcommand here so each subcommand can
  # easily use ARGV.shift to access its own subcommands.
  ARGV.shift

  Kaiser::Config.load(Dir.pwd)

  # We do all this work in here instead of the exe/kaiser file because we
  # want -h options to output before we check if a Kaiserfile exists.
  # If we do it in exe/kaiser, people won't be able to check help messages
  # unless they create a Kaiserfile firest.
  if opts[:quiet]
    Config.out = File.open(File::NULL, 'w')
    Config.info_out = File.open(File::NULL, 'w')
  elsif opts[:verbose] || Config.always_verbose?
    Config.out = $stderr
    Config.info_out = Kaiser::AfterDotter.new(dotter: Kaiser::Dotter.new)
  else
    Config.out = Kaiser::Dotter.new
    Config.info_out = Kaiser::AfterDotter.new(dotter: Config.out)
  end

  cmd.set_config

  cmd.execute(opts)
end

Instance Method Details

#define_options(global_opts = []) ⇒ Object

At first I did this in the constructor but the problem with that is Optimist will parse the entire commandline for the first Cli command registered. That means no matter what you call -h or –help on, it will always return the help for the first subcommand. Fixed this by only running define_options when a command is run. We can’t just run the constructor at that point because we need each Cli class to be constructed in the beginning so we can add their usage text to the output of ‘kaiser -h`.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kaiser/cli.rb', line 31

def define_options(global_opts = [])
  # We can't just call usage within the options block because that actually shifts
  # the scope to Optimist::Parser. We can still reference variables but we can't
  # call instance methods of a Kaiser::Cli class.
  u = usage
  Optimist.options do
    banner u

    global_opts.each { |o| opt *o }
  end
end

#set_configObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kaiser/cli.rb', line 10

def set_config
  # This is here for backwards compatibility since it can be used in Kaiserfiles.
  # It would be a good idea to deprecate this and make it more abstract.
  @work_dir = Config.work_dir
  @config_dir = Config.work_dir
  @config_file = Config.config_file
  @kaiserfile = Config.kaiserfile
  @config = Config.config
  @out = Config.out
  @info_out = Config.info_out

  @kaiserfile.validate!
end

#start_servicesObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/kaiser/cli.rb', line 102

def start_services
  services.each do |service|
    Config.info_out.puts "Starting service: #{service.name}"
    run_if_dead(
      service.shared_name,
      "docker run -d
        --name #{service.shared_name}
        --network #{Config.config[:networkname]}
        #{service.image}"
    )
  end
end

#stop_appObject



96
97
98
99
100
# File 'lib/kaiser/cli.rb', line 96

def stop_app
  Config.info_out.puts 'Stopping application'
  killrm app_container_name
  stop_services
end

#stop_servicesObject



115
116
117
118
119
120
# File 'lib/kaiser/cli.rb', line 115

def stop_services
  services.each do |service|
    Config.info_out.puts "Stopping service: #{service.name}"
    killrm service.shared_name
  end
end