Sha256: bd3d7de483b329cc8283ec9a5f8b90bcb14fca53f489216683f94ccb84e21052

Contents?: true

Size: 1.1 KB

Versions: 11

Compression:

Stored size: 1.1 KB

Contents

require 'aptible/auth'

module Aptible
  module CLI
    module Helpers
      module Token
        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

        # rubocop:disable MethodLength
        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
        # rubocop:enable MethodLength

        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

11 entries across 11 versions & 1 rubygems

Version Path
aptible-cli-0.4.1 lib/aptible/cli/helpers/token.rb
aptible-cli-0.4.0 lib/aptible/cli/helpers/token.rb
aptible-cli-0.3.7 lib/aptible/cli/helpers/token.rb
aptible-cli-0.3.6 lib/aptible/cli/helpers/token.rb
aptible-cli-0.3.5 lib/aptible/cli/helpers/token.rb
aptible-cli-0.3.4 lib/aptible/cli/helpers/token.rb
aptible-cli-0.3.3 lib/aptible/cli/helpers/token.rb
aptible-cli-0.3.2 lib/aptible/cli/helpers/token.rb
aptible-cli-0.3.1 lib/aptible/cli/helpers/token.rb
aptible-cli-0.3.0 lib/aptible/cli/helpers/token.rb
aptible-cli-0.2.0 lib/aptible/cli/helpers/token.rb