Module: Ey::Core::Cli::Helpers::Core
- Included in:
- Subcommand
- Defined in:
- lib/ey-core/cli/helpers/core.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #core_account ⇒ Object
- #core_accounts ⇒ Object
- #core_application_for(environment, options = {}) ⇒ Object
- #core_client ⇒ Object
- #core_environment_for(options = {}) ⇒ Object
- #core_operator_and_environment_for(options = {}) ⇒ Object
- #core_server_for(options = {}) ⇒ Object
- #core_url ⇒ Object
- #core_yaml ⇒ Object
- #eyrc_yaml ⇒ Object
- #longest_length_by_name(collection) ⇒ Object
- #operator(options) ⇒ Object
- #unauthenticated_core_client ⇒ Object
- #write_core_yaml(token = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
189 190 191 |
# File 'lib/ey-core/cli/helpers/core.rb', line 189 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#core_account ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ey-core/cli/helpers/core.rb', line 140 def core_account @_core_account ||= begin if [:account] found = core_client.accounts.get([:account]) || core_client.users.current.accounts.first(name: [:account]) if ENV["STAFF"] found ||= core_client.accounts.first(name: [:account]) end unless found = "Couldn't find account '#{[:account]}'" if core_client.users.current.staff && !ENV["STAFF"] += " (set environment variable STAFF=1 to search all accounts)" end raise end found else if core_accounts.size == 1 core_accounts.first else raise "Please specify --account (options: #{core_accounts.map(&:name).join(', ')})" end end end end |
#core_accounts ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/ey-core/cli/helpers/core.rb', line 166 def core_accounts @_core_accounts ||= begin if ENV["STAFF"] core_client.accounts else core_client.users.current.accounts end end end |
#core_application_for(environment, options = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 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/helpers/core.rb', line 78 def core_application_for(environment, ={}) candidate_apps = nil unless [:app] candidate_apps = environment.applications.map(&:name) if candidate_apps.size == 1 [:app] = candidate_apps.first else raise "--app is required (Candidate apps on environment #{environment.name}: #{candidate_apps.join(', ')})" end end app = begin Integer([:app]) rescue [:app] end if app.is_a?(Integer) environment.applications.get(app) else applications = environment.applications.all(name: app) if applications.count == 1 applications.first else error_msg = [ "Found multiple applications that matched that search.", "Please be more specific by specifying the account, environment, and application name.", "Matching applications: #{applications.map(&:name)}.", ] if candidate_apps error_msg << "applications on this environment: #{candidate_apps}" end raise Ey::Core::Cli::AmbiguousSearch.new(error_msg.join(" ")) end end end |
#core_client ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/ey-core/cli/helpers/core.rb', line 119 def core_client @core_client ||= begin opts = {url: core_url, config_file: self.class.core_file} opts.merge!(token: ENV["CORE_TOKEN"]) if ENV["CORE_TOKEN"] if ENV["DEBUG"] opts[:logger] = ::Logger.new(STDOUT) end Ey::Core::Client.new(opts) end rescue RuntimeError => e if legacy_token = e..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..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
69 70 71 |
# File 'lib/ey-core/cli/helpers/core.rb', line 69 def core_environment_for(={}) core_client.environments.get([:environment]) || core_client.environments.first(name: [:environment]) end |
#core_operator_and_environment_for(options = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ey-core/cli/helpers/core.rb', line 46 def core_operator_and_environment_for(={}) unless [:environment] raise "--environment is required (for a list of environments, try `ey environments`)" end operator = operator() environment = nil if [:environment].to_i.to_s == [:environment] environment = operator.environments.get([:environment]) end unless environment candidate_envs = operator.environments.all(name: [:environment]) if candidate_envs.size > 1 raise "Multiple matching environments found named '#{[:environment]}', please specify --account" else environment = candidate_envs.first end end unless environment raise "environment '#{[:environment]}' not found (for a list of environments, try `ey environments`)" end [operator, environment] end |
#core_server_for(options = {}) ⇒ Object
73 74 75 76 |
# File 'lib/ey-core/cli/helpers/core.rb', line 73 def core_server_for(={}) operator = .fetch(:operator, core_client) operator.servers.get([:server]) || operator.servers.first(provisioned_id: [:server]) end |
#core_url ⇒ Object
19 20 21 22 |
# File 'lib/ey-core/cli/helpers/core.rb', line 19 def core_url env_url = ENV["CORE_URL"] || ENV["CLOUD_URL"] (env_url && File.join(env_url, '/')) || "https://api.engineyard.com/" end |
#core_yaml ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/ey-core/cli/helpers/core.rb', line 28 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 |
#eyrc_yaml ⇒ Object
183 184 185 186 187 |
# File 'lib/ey-core/cli/helpers/core.rb', line 183 def eyrc_yaml @eyrc_yaml ||= YAML.load_file(self.class.eyrc) || {} rescue Errno::ENOENT # we don't really care if this doesn't exist {} end |
#longest_length_by_name(collection) ⇒ Object
24 25 26 |
# File 'lib/ey-core/cli/helpers/core.rb', line 24 def longest_length_by_name(collection) collection.map(&:name).group_by(&:size).max.last.length end |
#operator(options) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/ey-core/cli/helpers/core.rb', line 36 def operator() if [:account] core_account elsif ENV["STAFF"] core_client else core_client.users.current end end |
#unauthenticated_core_client ⇒ Object
115 116 117 |
# File 'lib/ey-core/cli/helpers/core.rb', line 115 def unauthenticated_core_client @unauthenticated_core_client ||= Ey::Core::Client.new(token: nil, url: core_url) end |
#write_core_yaml(token = nil) ⇒ Object
176 177 178 179 180 181 |
# File 'lib/ey-core/cli/helpers/core.rb', line 176 def write_core_yaml(token=nil) core_yaml[core_url] = token if token File.open(self.class.core_file, "w") {|file| file.puts core_yaml.to_yaml } end |