Sha256: 7fbea3ae28256a6d54daea338bf723e8f62c0855ae2fa7935bf8859d24387bd1

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

require 'aptible/api'
require 'git'

module Aptible
  module CLI
    module Helpers
      module Account
        include Helpers::Token

        def scoped_accounts(options)
          if options[:account]
            if (account = account_from_handle(options[:account]))
              [account]
            else
              fail Thor::Error, 'Specified account does not exist'
            end
          else
            Aptible::Api::Account.all(token: fetch_token)
          end
        end

        def ensure_account(options = {})
          if (handle = options[:account])
            account = account_from_handle(handle)
            return account if account
            fail "Could not find account #{handle}"
          else
            ensure_default_account
          end
        end

        def account_from_handle(handle)
          Aptible::Api::Account.all(token: fetch_token).find do |a|
            a.handle == handle
          end
        end

        def ensure_default_account
          accounts = Aptible::Api::Account.all(token: fetch_token)
          return accounts.first if accounts.count == 1

          fail Thor::Error, <<-ERR.gsub(/\s+/, ' ').strip
            Multiple accounts available, please specify with --account
          ERR
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aptible-cli-0.5.15 lib/aptible/cli/helpers/account.rb
aptible-cli-0.5.14 lib/aptible/cli/helpers/account.rb