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

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

Instance Method Summary collapse

Instance Method Details

#current_token_hashObject

rubocop:enable MethodLength



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

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

rubocop:disable MethodLength



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

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



37
38
39
# File 'lib/aptible/cli/helpers/token.rb', line 37

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