Class: GCE::Host::CLI
- Inherits:
-
Object
- Object
- GCE::Host::CLI
- Defined in:
- lib/gce/host/cli.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(argv = ARGV) ⇒ CLI
constructor
A new instance of CLI.
- #parse_options(argv = ARGV) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(argv = ARGV) ⇒ CLI
Returns a new instance of CLI
9 10 11 |
# File 'lib/gce/host/cli.rb', line 9 def initialize(argv = ARGV) @options = (argv) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options
7 8 9 |
# File 'lib/gce/host/cli.rb', line 7 def @options end |
Instance Method Details
#parse_options(argv = ARGV) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 |
# File 'lib/gce/host/cli.rb', line 13 def (argv = ARGV) op = OptionParser.new self.class.module_eval do define_method(:usage) do |msg = nil| puts op.to_s puts "error: #{msg}" if msg exit 1 end end opts = { state: ["running"] } op.on('--hostname one,two,three', Array, "name") {|v| opts[:hostname] = v } op.on('-r', '--role one,two,three', Array, "role") {|v| opts[:role] = v } 1.upto(Config.role_max_depth).each do |i| op.on("--r#{i}", "--role#{i} one,two,three", Array, "role#{i}, #{i}th part of role delimited by #{Config.role_value_delimiter}") {|v| opts["role#{i}".to_sym] = v } end op.on('--instance-id one,two,three', Array, "instance_id") {|v| opts[:instance_id] = v } op.on("--#{Config.status} one,two,three", Array, "filter with instance #{Config.status} (default: running)") {|v| opts[Config.status.to_sym] = v } Config..keys.each do |opt| op.on("--#{opt.to_s.gsub('_', '-')} one,two,three", Array, opt) {|v| opts[opt.to_sym] = v } end op.on('-a', '--all', "list all hosts (remove default filter)") {|v| [:hostname, :role, :instance_id, Config.status.to_sym].each do |key| opts.delete(key) end 1.upto(Config.role_max_depth).each do |i| opts.delete("role#{i}".to_sym) end Config..keys.each do |opt| opts.delete(opt.to_sym) end } op.on('--private-ip', '--ip', "show private ip address instead of hostname") {|v| opts[:private_ip] = v } op.on('--public-ip', "show public ip address instead of hostname") {|v| opts[:public_ip] = v } op.on('-i', '--info', "show host info") {|v| opts[:info] = v } op.on('-j', '--jsonl', "show host info in line delimited json") {|v| opts[:jsonl] = v } op.on('--json', "show host info in json") {|v| opts[:json] = v } op.on('--pretty-json', "show host info in pretty json") {|v| opts[:pretty_json] = v } op.on('--debug', "debug mode") {|v| opts[:debug] = v } op.on('-h', '--help', "show help") {|v| opts[:help] = v } begin args = op.parse(argv) rescue OptionParser::InvalidOption => e usage e. end if opts[:help] usage end opts end |
#run ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/gce/host/cli.rb', line 99 def run hosts = GCE::Host.new(condition).sort_by {|host| host.hostname } if [:info] hosts.each do |host| $stdout.puts host.info end elsif [:jsonl] hosts.each do |host| $stdout.puts host.to_hash.to_json end elsif [:json] $stdout.puts hosts.map(&:to_hash).to_json elsif [:pretty_json] $stdout.puts JSON.pretty_generate(hosts.map(&:to_hash)) elsif [:private_ip] hosts.each do |host| $stdout.puts host.private_ip_address end elsif [:public_ip] hosts.each do |host| $stdout.puts host.public_ip_address end else hosts.each do |host| $stdout.puts host.hostname end end end |