Class: Ey::Core::Cli::Subcommand

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.core_fileObject

Returns the value of attribute core_file.



7
8
9
# File 'lib/ey-core/cli/subcommand.rb', line 7

def core_file
  @core_file
end

Class Method Details

.descendantsObject



2
3
4
# File 'lib/ey-core/cli/subcommand.rb', line 2

def self.descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

.eyrcObject



13
14
15
# File 'lib/ey-core/cli/subcommand.rb', line 13

def eyrc
  @eyrc ||= File.expand_path("~/.eyrc")
end

Instance Method Details

#core_account_for(options = {}) ⇒ Object



68
69
70
71
# File 'lib/ey-core/cli/subcommand.rb', line 68

def (options={})
   ||= core_client.accounts.get(options[:account])
   ||= core_client.users.current.accounts.first(name: options[:account])
end

#core_application_for(options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ey-core/cli/subcommand.rb', line 92

def core_application_for(options={})
  return nil unless options[:app]

  app = begin
          Integer(options[:app])
        rescue
          options[:app]
        end

  actor = options[:environment].is_a?(Ey::Core::Client::Environment) ? options[:environment]. : operator(options)

  if app.is_a?(Integer)
    actor.applications.get(app)
  else
    applications = actor.applications.all(name: app)
    if applications.count == 1
      applications.first
    else
      raise Ey::Core::Cli::AmbiguousSearch.new("Found multiple applications that matched that search.  Please be more specific by specifying the account, environment, and application name.")
    end
  end
end

#core_clientObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ey-core/cli/subcommand.rb', line 22

def core_client
  @core_client ||= Ey::Core::Client.new(url: core_url, config_file: self.class.core_file)
rescue RuntimeError => e
  if legacy_token = e.message.match(/missing token/i) && eyrc_yaml["api_token"]
    puts "Found legacy .eyrc token.  Migrating to core file".green
    write_core_yaml(legacy_token)
    retry
  elsif e.message.match(/missing token/i)
    abort "Missing credentials: Run 'ey login' to retrieve your Engine Yard Cloud API token.".yellow
  else
    raise e
  end
end

#core_environment_for(options = {}) ⇒ Object



83
84
85
# File 'lib/ey-core/cli/subcommand.rb', line 83

def core_environment_for(options={})
  core_client.environments.get(options[:environment]) || core_client.environments.first(name: options[:environment])
end

#core_operator_and_environment_for(options = {}) ⇒ Object



77
78
79
80
81
# File 'lib/ey-core/cli/subcommand.rb', line 77

def core_operator_and_environment_for(options={})
  operator = operator(options)
  environment = operator.environments.get(options[:environment]) || operator.environments.first(name: options[:environment])
  [operator, environment]
end

#core_server_for(options = {}) ⇒ Object



87
88
89
90
# File 'lib/ey-core/cli/subcommand.rb', line 87

def core_server_for(options={})
  operator = options.fetch(:operator, core_client)
  operator.servers.get(options[:server]) || operator.servers.first(provisioned_id: options[:server])
end

#core_urlObject



36
37
38
39
# File 'lib/ey-core/cli/subcommand.rb', line 36

def core_url
  env_url = ENV["CORE_URL"] || ENV["CLOUD_URL"]
  (env_url && File.join(env_url, '/')) || "https://api.engineyard.com/"
end

#core_yamlObject



60
61
62
63
64
65
66
# File 'lib/ey-core/cli/subcommand.rb', line 60

def core_yaml
  @core_yaml ||= YAML.load_file(self.class.core_file) || {}
rescue Errno::ENOENT => e
  puts "Creating #{self.class.core_file}".yellow
  FileUtils.touch(self.class.core_file)
  retry
end

#current_accountsObject



41
42
43
# File 'lib/ey-core/cli/subcommand.rb', line 41

def current_accounts
  core_client.users.current.accounts
end

#eyrc_yamlObject



54
55
56
57
58
# File 'lib/ey-core/cli/subcommand.rb', line 54

def eyrc_yaml
  @eyrc_yaml ||= YAML.load_file(self.class.eyrc) || {}
rescue Errno::ENOENT => e # we don't really care if this doesn't exist
  {}
end

#longest_length_by_name(collection) ⇒ Object



45
46
47
# File 'lib/ey-core/cli/subcommand.rb', line 45

def longest_length_by_name(collection)
  collection.map(&:name).group_by(&:size).max.last.length
end

#operator(options) ⇒ Object



73
74
75
# File 'lib/ey-core/cli/subcommand.rb', line 73

def operator(options)
  options[:account] ? (options) : core_client
end

#unauthenticated_core_clientObject



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

def unauthenticated_core_client
  @unauthenticated_core_client ||= Ey::Core::Client.new(token: nil, url: core_url)
end

#write_core_yaml(token = nil) ⇒ Object



49
50
51
52
# File 'lib/ey-core/cli/subcommand.rb', line 49

def write_core_yaml(token=nil)
  core_yaml[core_url] = token if token
  File.open(self.class.core_file, "w") { |f| f.puts core_yaml.to_yaml }
end