Class: Collins::CLI::State

Inherits:
Object
  • Object
show all
Includes:
Formatter, Mixins
Defined in:
lib/collins/cli/state.rb

Constant Summary collapse

PROG_NAME =
'collins state'
OPTIONS_DEFAULTS =
{
  :format => :table,
  :separator => "\t",
  :timeout => 120,
  :show_header => false,
  :config => nil
}

Constants included from Formatter

Formatter::ADDRESS_POOL_COLUMNS, Formatter::FORMATTING_DEFAULTS, Formatter::STATUS_STATE_COLUMNS

Constants included from Mixins

Mixins::COLORS, Mixins::ERROR, Mixins::SUCCESS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Formatter

#display_as_link, #display_as_robot_talk, #display_as_table, #format_assets, #format_pools, #format_states

Methods included from Mixins

#api_call, #as_query?, #collins, #convert_to_query

Constructor Details

#initializeState

Returns a new instance of State.



18
19
20
21
22
# File 'lib/collins/cli/state.rb', line 18

def initialize
  @options = OPTIONS_DEFAULTS.clone
  @validated = false
  @parsed = false
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/collins/cli/state.rb', line 16

def options
  @options
end

Instance Method Details

#parse!(argv = ARGV) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/collins/cli/state.rb', line 24

def parse!(argv = ARGV)
  OptionParser.new do |opts|
    opts.banner = "Usage: #{PROG_NAME} [options]"
    opts.on('-l','--list',"List states.") { @options[:mode] = :list }
    opts.on('-h','--help',"Help") {puts opts ; exit 0}
    opts.separator ""
    opts.separator "Table formatting:"
    opts.on('-H','--show-header',"Show header fields in output") {options[:show_header] = true}
    opts.on('-f','--field-separator SEPARATOR',String,"Separator between columns in output (Default: #{options[:separator]})") {|v| options[:separator] = v}
    opts.separator ""
    opts.separator "Extra options:"
    opts.on('--timeout SECONDS',Integer,"Timeout in seconds (0 == forever)") {|v| options[:timeout] = v}
    opts.on('-C','--config CONFIG',String,'Use specific Collins config yaml for Collins::Client') {|v| options[:config] = v}
    opts.separator ""
    opts.separator "Examples:"
    opts.separator "  Show states and statuses:\n\#{PROG_NAME} --list\n"
  end.parse!(argv)
  @parsed = true
  self
end

#run!Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/collins/cli/state.rb', line 54

def run!
  exit_clean = true
  case @options[:mode]
  when :list
    states = collins.state_get_all
    format_states(states, options)
  else
    raise "I dunno what you want me to do! See #{PROG_NAME} --help"
  end
  exit_clean

end

#validate!Object



48
49
50
51
52
# File 'lib/collins/cli/state.rb', line 48

def validate!
  raise "See --help for #{PROG_NAME} usage" if options[:mode].nil?
  @validated = true
  self
end