Class: Ey::Core::Cli
- Inherits:
-
Object
- Object
- Ey::Core::Cli
- Defined in:
- lib/vendor/core/ey-core/cli.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Class Method Summary collapse
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(args = ARGV) ⇒ Cli
constructor
A new instance of Cli.
- #parse(args) ⇒ Object
- #run ⇒ Object
- #set_client(options) ⇒ Object
- #set_resource(action, resource_name, resource_id) ⇒ Object
- #show ⇒ Object
Constructor Details
#initialize(args = ARGV) ⇒ Cli
20 21 22 |
# File 'lib/vendor/core/ey-core/cli.rb', line 20 def initialize(args=ARGV) @action, = parse(args) end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
18 19 20 |
# File 'lib/vendor/core/ey-core/cli.rb', line 18 def action @action end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
18 19 20 |
# File 'lib/vendor/core/ey-core/cli.rb', line 18 def client @client end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
18 19 20 |
# File 'lib/vendor/core/ey-core/cli.rb', line 18 def end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
18 19 20 |
# File 'lib/vendor/core/ey-core/cli.rb', line 18 def resource @resource end |
Class Method Details
.say(*args) ⇒ Object
14 15 16 |
# File 'lib/vendor/core/ey-core/cli.rb', line 14 def self.say(*args) self.stdout.puts(*args) end |
.stdout ⇒ Object
10 11 12 |
# File 'lib/vendor/core/ey-core/cli.rb', line 10 def self.stdout @stdout ||= STDOUT end |
Instance Method Details
#destroy ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/vendor/core/ey-core/cli.rb', line 79 def destroy response = resource.destroy! if response.is_a?(Ey::Core::Client::Request) response.wait_for!(response.service.timeout, response.service.poll_interval) do |request| Ey::Core::Cli.say "Waiting ... #{Time.now}" Ey::Core::Cli.say(response.ai) unless request.ready? request.ready? end end Ey::Core::Cli.say(response.ai) end |
#parse(args) ⇒ Object
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 |
# File 'lib/vendor/core/ey-core/cli.rb', line 24 def parse(args) = {} action, resource_name, resource_id = OptionParser.new do |opts| opts. = "Usage: ey-core ACTION RESOURCE RESOURCE_ID [options]" opts.separator "" opts.separator "Specific options:" opts.on("-t", "--token [TOKEN]", "Use specific token. Defaults to core url entry in ~/.ey-core yaml file") do |token| [:token] = token end opts.on("-u", "--url [URL]", "Use specific core URL. Defaults to 'https://api.engineyard.com'") do |url| [:url] = url end opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| [:logger] = v ? Logger.new(STDOUT) : Logger.new(nil) end opts.separator "" opts.separator "Common options:" # No argument, shows at tail. This will print an options summary. # Try it and see! opts.on_tail("-h", "--help", "Show this message") do puts opts exit end # Another typical switch to print the version. opts.on_tail("--version", "Show version") do puts Ey::Core::VERSION exit end end.parse!(args) set_client() set_resource(action, resource_name, resource_id) [action, (.to_hash || {})] end |
#run ⇒ Object
71 72 73 |
# File 'lib/vendor/core/ey-core/cli.rb', line 71 def run public_send(action) end |
#set_client(options) ⇒ Object
93 94 95 |
# File 'lib/vendor/core/ey-core/cli.rb', line 93 def set_client() @client ||= Ey::Core::Client.new() end |
#set_resource(action, resource_name, resource_id) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/vendor/core/ey-core/cli.rb', line 97 def set_resource(action, resource_name, resource_id) action || raise(ArgumentError.new("Missing action")) Ey::Core::Client.models.find { |m,_| m.to_s == resource_name } || raise(ArgumentError.new("Unknown resource: #{resource_name}")) %w[show destroy].include?(action) || raise(ArgumentError.new("Unknown action: #{action}")) # TODO: irregular inflections and all that collection = "#{resource_name.to_s}s" @resource = client.public_send(collection).get!(resource_id) end |
#show ⇒ Object
75 76 77 |
# File 'lib/vendor/core/ey-core/cli.rb', line 75 def show Ey::Core::Cli.say(resource.ai) end |