Sha256: d1f10e95ec1617f44d0d4c9d14617397d303d46c47ae928fd98e7713173c6ec2

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

Stored size: 1.17 KB

Contents

require 'aptible/auth'

module Aptible
  module CLI
    module Helpers
      module Token
        TOKEN_ENV_VAR = 'APTIBLE_ACCESS_TOKEN'

        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

        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

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
aptible-cli-0.7.1 lib/aptible/cli/helpers/token.rb
aptible-cli-0.7.0 lib/aptible/cli/helpers/token.rb
aptible-cli-0.6.9 lib/aptible/cli/helpers/token.rb
aptible-cli-0.6.8 lib/aptible/cli/helpers/token.rb
aptible-cli-0.6.7 lib/aptible/cli/helpers/token.rb
aptible-cli-0.6.6 lib/aptible/cli/helpers/token.rb