Class: Ey::Core::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/core/ey-core/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ Cli



20
21
22
# File 'lib/vendor/core/ey-core/cli.rb', line 20

def initialize(args=ARGV)
  @action, @options = parse(args)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



18
19
20
# File 'lib/vendor/core/ey-core/cli.rb', line 18

def action
  @action
end

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'lib/vendor/core/ey-core/cli.rb', line 18

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/vendor/core/ey-core/cli.rb', line 18

def options
  @options
end

#resourceObject (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

.stdoutObject



10
11
12
# File 'lib/vendor/core/ey-core/cli.rb', line 10

def self.stdout
  @stdout ||= STDOUT
end

Instance Method Details

#destroyObject



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)
  options = {}

  action, resource_name, resource_id = OptionParser.new do |opts|
    opts.banner = "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|
      options[:token] = token
    end

    opts.on("-u", "--url [URL]",
            "Use specific core URL. Defaults to 'https://api.engineyard.com'") do |url|
      options[:url] = url
    end

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options[: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(options)

  set_resource(action, resource_name, resource_id)

  [action, (options.to_hash || {})]
end

#runObject



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(options)
  @client ||= Ey::Core::Client.new(options)
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

#showObject



75
76
77
# File 'lib/vendor/core/ey-core/cli.rb', line 75

def show
  Ey::Core::Cli.say(resource.ai)
end