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, #decode_token, #fetch_token, #save_token, #token_file

Methods included from ConfigPath

#aptible_config_path

Instance Method Details

#ensure_default_environmentObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aptible/cli/helpers/environment.rb', line 59

def ensure_default_environment
  href = environment_href
  environments = Aptible::Api::Account.all(
    token: fetch_token,
    href: href
  )
  case environments.count
  when 0
    e = 'No environments. Go to https://app.aptible.com/ to proceed'
    raise Thor::Error, e
  when 1
    return environments.first
  else
    raise Thor::Error, <<-ERR.gsub(/\s+/, ' ').strip
      Multiple environments available, please specify with --environment or --env
    ERR
  end
end

#ensure_environment(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/aptible/cli/helpers/environment.rb', line 33

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

#environment_from_handle(handle) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/aptible/cli/helpers/environment.rb', line 43

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

#environment_hrefObject



9
10
11
12
13
14
15
# File 'lib/aptible/cli/helpers/environment.rb', line 9

def environment_href
  href = '/accounts'
  if Renderer.format != 'json'
    href = '/accounts?per_page=5000&no_embed=true'
  end
  href
end

#environment_map(accounts) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/aptible/cli/helpers/environment.rb', line 51

def environment_map(accounts)
  acc_map = {}
  accounts.each do ||
    acc_map[.links.self.href] = 
  end
  acc_map
end

#scoped_environments(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aptible/cli/helpers/environment.rb', line 17

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