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 |
# 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 } op.on('--r1', '--role1 one,two,three', Array, "role1, the 1st part of role delimited by #{Config.role_tag_delimiter}") {|v| opts[:role1] = v } op.on('--r2', '--role2 one,two,three', Array, "role2, the 2st part of role delimited by #{Config.role_tag_delimiter}") {|v| opts[:role2] = v } op.on('--r3', '--role3 one,two,three', Array, "role3, the 3st part of role delimited by #{Config.role_tag_delimiter}") {|v| opts[:role3] = v } op.on('--instance-id one,two,three', Array, "instance_id") {|v| opts[:instance_id] = v } op.on('--state one,two,three', Array, "instance state (default: running)") {|v| opts[:state] = v } op.on('--monitoring one,two,three', Array, "monitoring") {|v| opts[:monitoring] = 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('--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
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 121 122 |
# File 'lib/ec2/host/cli.rb', line 95 def run hosts = EC2::Host.new(condition) 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 |