Module: Aptible::CLI::Helpers::Environment

Includes:
Token
Included in:
App, Database
Defined in:
lib/aptible/cli/helpers/environment.rb

Constant Summary

Constants included from Token

Token::TOKEN_ENV_VAR

Instance Method Summary collapse

Methods included from Token

#current_token_hash, #fetch_token, #save_token, #token_file

Instance Method Details

#ensure_default_environmentObject



39
40
41
42
43
44
45
46
# File 'lib/aptible/cli/helpers/environment.rb', line 39

def ensure_default_environment
  environments = Aptible::Api::.all(token: fetch_token)
  return environments.first if environments.count == 1

  fail Thor::Error, "Multiple environments available, please specify with --environment\n".gsub(/\s+/, ' ').strip
end

#ensure_environment(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/aptible/cli/helpers/environment.rb', line 22

def ensure_environment(options = {})
  if (handle = options[:environment])
    environment = environment_from_handle(handle)
    return environment if environment
    fail Thor::Error, "Could not find environment #{handle}"
  else
    ensure_default_environment
  end
end

#environment_from_handle(handle) ⇒ Object



32
33
34
35
36
37
# File 'lib/aptible/cli/helpers/environment.rb', line 32

def environment_from_handle(handle)
  return nil unless handle
  Aptible::Api::.all(token: fetch_token).find do |a|
    a.handle == handle
  end
end

#scoped_environments(options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/aptible/cli/helpers/environment.rb', line 10

def scoped_environments(options)
  if options[:environment]
    if (environment = environment_from_handle(options[:environment]))
      [environment]
    else
      fail Thor::Error, 'Specified account does not exist'
    end
  else
    Aptible::Api::.all(token: fetch_token)
  end
end