Method: Collins::CLI::Find#parse!

Defined in:
lib/collins/cli/find.rb

#parse!(argv = ARGV) ⇒ Object



38
39
40
41
42
43
44
45
46
47
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/collins/cli/find.rb', line 38

def parse!(argv = ARGV)
  raise "See --help for #{PROG_NAME} usage" if argv.empty?
  @parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{PROG_NAME} [options] [hostnamepattern]"
    opts.separator "Query options:"
    opts.on('-t','--tag TAG[,...]',Array, "Assets with tag[s] TAG") {|v| search_attrs[:tag] = v}
    opts.on('-Z','--remote-lookup',"Query remote datacenters for asset (Default: #{query_opts[:remoteLookup]})") {|v| search_attrs[:remoteLookup] = v}
    opts.on('-T','--type TYPE',String, "Only show assets with type TYPE") {|v| search_attrs[:type] = v}
    opts.on('-n','--nodeclass NODECLASS[,...]',Array, "Assets in nodeclass NODECLASS") {|v| search_attrs[:nodeclass] = v}
    opts.on('-p','--pool POOL[,...]',Array, "Assets in pool POOL") {|v| search_attrs[:pool] = v}
    opts.on('-s','--size SIZE',Integer, "Number of assets to return per page (Default: #{query_opts[:size]})") {|v| query_opts[:size] = v}
    opts.on('--limit NUM', Integer, "Limit total results to NUM of assets") { |v| options[:limit] = v}
    opts.on('-r','--role ROLE[,...]',Array,"Assets in primary role ROLE") {|v| search_attrs[:primary_role] = v}
    opts.on('-R','--secondary-role ROLE[,...]',Array,"Assets in secondary role ROLE") {|v| search_attrs[:secondary_role] = v}
    opts.on('-i','--ip-address IP[,...]',Array,"Assets with IP address[es]") {|v| search_attrs[:ip_address] = v}
    opts.on('-S','--status STATUS[:STATE][,...]',Array,"Asset status (and optional state after :)") do |v|
      # in order to know what state was paired with what status, lets store the original params
      # so the query constructor can create the correct CQL query
      options[:status_state] = v
      search_attrs[:status], search_attrs[:state] = v.inject([[],[]]) do |memo,s|
        status,state = s.split(':')
        memo[0] << status.upcase if not status.nil? and not status.empty?
        memo[1] << state.upcase if not state.nil? and not state.empty?
        memo
      end
    end
    opts.on('-a','--attribute attribute[:value[,...]]',String,"Arbitrary attributes and values to match in query. : between key and value") do |x|
      x.split(',').each do |p|
        a,v = p.split(':', 2) # attribute:value where value might contain :s
        a = a.to_sym
        if not search_attrs[a].nil? and not search_attrs[a].is_a? Array
          # its a single value, turn it into an array
          search_attrs[a] = [search_attrs[a]]
        end
        if search_attrs[a].is_a? Array
          # already multivalue, append
          search_attrs[a] << v
        else
          search_attrs[a] = v
        end
      end
    end

    opts.separator ""
    opts.separator "Table formatting:"
    opts.on('-H','--show-header',"Show header fields in output") {options[:show_header] = true}
    opts.on('-c','--columns ATTRIBUTES',Array,"Attributes to output as columns, comma separated (Default: #{options[:columns].map(&:to_s).join(',')})") {|v| options[:column_override] = v.map(&:to_sym)}
    opts.on('-x','--extra-columns ATTRIBUTES',Array,"Show these columns in addition to the default columns, comma separated") {|v| options[:columns].push(v.map(&:to_sym)).flatten! }
    opts.on('-f','--field-separator SEPARATOR',String,"Separator between columns in output (Default: #{options[:separator]})") {|v| options[:separator] = v}

    opts.separator ""
    opts.separator "Robot formatting:"
    opts.on('-l','--link',"Output link to assets found in web UI") {options[:format] = :link}
    opts.on('-j','--json',"Output results in JSON (NOTE: This probably wont be what you expected)") {options[:format] = :json}
    opts.on('-y','--yaml',"Output results in YAML") {options[:format] = :yaml}

    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.on('-h','--help',"Help") {options[:mode] = :help}

    opts.separator ""
    opts.separator "Examples:\nQuery for devnodes in DEVEL pool that are VMs\n  \#{PROG_NAME} -n develnode -p DEVEL\nQuery for asset 001234, and show its system_password\n  \#{PROG_NAME} -t 001234 -x system_password\nQuery for all decommissioned VM assets\n  \#{PROG_NAME} -a is_vm:true -S decommissioned\nQuery for hosts matching hostname '^web6-'\n  \#{PROG_NAME} ^web6-\nQuery for all develnode6 nodes with a value for PUPPET_SERVER\n  \#{PROG_NAME} -n develnode6 -a puppet_server -H\n"
  end
  @parser.parse!(argv)
  # hostname is the final option, no flags
  search_attrs[:hostname] = argv.shift
  @parsed = true
  self
end