Module: Aptible::CLI::Subcommands::Inspect

Included in:
Agent
Defined in:
lib/aptible/cli/subcommands/inspect.rb

Defined Under Namespace

Classes: InspectResourceCommand

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/aptible/cli/subcommands/inspect.rb', line 40

def self.included(thor)
  # We have to manually register a command here since we can't override
  # the inspect method!
  desc = 'Inspect a resource as JSON by URL'
  thor.commands['inspect'] = InspectResourceCommand.new(
    :inspect, desc, desc, 'inspect URL'
  )
end

Instance Method Details

#inspect_resource(raw) ⇒ Object



11
12
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
# File 'lib/aptible/cli/subcommands/inspect.rb', line 11

def inspect_resource(raw)
  begin
    uri = URI(raw)
  rescue URI::InvalidURIError
    raise Thor::Error, "Invalid URI: #{raw}"
  end

  if uri.scheme != 'https'
    raise "Invalid scheme: #{uri.scheme} (use https)"
  end

  apis = [Aptible::Auth, Aptible::Api]

  api = apis.find do |klass|
    uri.host == URI(klass.configuration.root_url).host
  end

  if api.nil?
    hosts = apis.map(&:configuration).map(&:root_url).map do |u|
      URI(u).host
    end
    m = "Invalid API: #{uri.host} (valid APIs: #{hosts.join(', ')})"
    raise Thor::Error, m
  end

  res = api::Resource.new(token: fetch_token).find_by_url(uri.to_s)
  puts JSON.pretty_generate(res.body)
end