Module: Aptible::CLI::Helpers::Token

Included in:
Agent, App, Database, Environment
Defined in:
lib/aptible/cli/helpers/token.rb

Constant Summary collapse

TOKEN_ENV_VAR =
'APTIBLE_ACCESS_TOKEN'

Instance Method Summary collapse

Instance Method Details

#current_token_hashObject



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

def current_token_hash
  JSON.parse(File.read(token_file))
rescue
  {}
end

#fetch_tokenObject



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

def fetch_token
  @token ||= ENV[TOKEN_ENV_VAR] ||
             current_token_hash[Aptible::Auth.configuration.root_url]
  return @token if @token
  fail Thor::Error, 'Could not read token: please run aptible login ' \
                    "or set #{TOKEN_ENV_VAR}"
end

#save_token(token) ⇒ Object



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

def save_token(token)
  hash = current_token_hash.merge(
    Aptible::Auth.configuration.root_url => token
  )

  FileUtils.mkdir_p(File.dirname(token_file))
  File.open(token_file, 'w') do |file|
    file.puts hash.to_json
  end
rescue
  raise Thor::Error, "    Could not write token to \#{token_file}, please check filesystem\n    permissions\n  ERR\nend\n".gsub(/\s+/, ' ').strip

#token_fileObject



39
40
41
# File 'lib/aptible/cli/helpers/token.rb', line 39

def token_file
  File.join ENV['HOME'], '.aptible', 'tokens.json'
end