Sha256: fdb6197f368f041ea6cf65da9954c8b03d1ad6b1105477559db17cdf1e13aa56

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

require 'aptible/auth'

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

        def fetch_token
          @token ||= ENV[TOKEN_ENV_VAR] ||
                     current_token_hash[Aptible::Auth.configuration.root_url]
          return @token if @token
          raise 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

8 entries across 8 versions & 1 rubygems

Version Path
aptible-cli-0.11.0 lib/aptible/cli/helpers/token.rb
aptible-cli-0.10.0 lib/aptible/cli/helpers/token.rb
aptible-cli-0.9.0 lib/aptible/cli/helpers/token.rb
aptible-cli-0.8.6 lib/aptible/cli/helpers/token.rb
aptible-cli-0.8.5 lib/aptible/cli/helpers/token.rb
aptible-cli-0.8.4 lib/aptible/cli/helpers/token.rb
aptible-cli-0.8.3 lib/aptible/cli/helpers/token.rb
aptible-cli-0.8.2 lib/aptible/cli/helpers/token.rb