Class: EC2::Host::CLI
- Inherits:
-
Object
- Object
- EC2::Host::CLI
- Defined in:
- lib/ec2/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/ec2/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/ec2/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 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ec2/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 or private_dns_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_tag_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('--state one,two,three', Array, "filter with instance state such as running, stopped (default: running)") {|v| opts[:state] = v } op.on('--monitoring one,two,three', Array, "filter with instance monitoring") {|v| opts[:monitoring] = v } op.on('--[no-]spot', "filter to spot or non-spot instances") {|v| opts[:spot] = v } Config..each do |opt, tag| 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, :state, :monitoring].each do |key| opts.delete(key) end 1.upto(Config.role_max_depth).each do |i| opts.delete("role#{i}".to_sym) end Config..each do |opt, tag| 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 } op.on('-v', '--version', "show version") {|v| puts EC2::Host::VERSION exit 0 } begin args = op.parse(argv) rescue OptionParser::InvalidOption => e usage e. end if opts[:help] usage end opts end |
#run ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/ec2/host/cli.rb', line 109 def run hosts = EC2::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 |