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

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

Instance Method Summary collapse

Instance Method Details

#current_token_hashObject



29
30
31
32
33
# File 'lib/aptible/cli/helpers/token.rb', line 29

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

#fetch_tokenObject



7
8
9
10
11
# File 'lib/aptible/cli/helpers/token.rb', line 7

def fetch_token
  @token ||= current_token_hash[Aptible::Auth.configuration.root_url]
  return @token if @token
  fail Thor::Error, 'Could not read token: please run aptible login'
end

#save_token(token) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/aptible/cli/helpers/token.rb', line 13

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, <<-ERR.gsub(/\s+/, ' ').strip
    Could not write token to #{token_file}, please check filesystem
    permissions
  ERR
end

#token_fileObject



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

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