class Comply::CLI::Agent

Public Class Methods

exit_on_failure?() click to toggle source

Forward return codes on failures.

# File lib/comply/cli/agent.rb, line 20
def self.exit_on_failure?
  true
end
new(*) click to toggle source
Calls superclass method
# File lib/comply/cli/agent.rb, line 24
def initialize(*)
  Aptible::Resource.configure { |conf| conf.user_agent = version_string }
  super
end

Public Instance Methods

login() click to toggle source
# File lib/comply/cli/agent.rb, line 40
def login
  email = options[:email] || ask('Email: ')
  password = options[:password] || ask('Password: ', echo: false)
  puts ''

  token_options = { email: email, password: password }

  otp_token = options[:otp_token]
  token_options[:otp_token] = otp_token if otp_token

  begin
    lifetime = '1w'
    lifetime = '12h' if token_options[:otp_token]
    lifetime = options[:lifetime] if options[:lifetime]

    duration = ChronicDuration.parse(lifetime)
    if duration.nil?
      raise Thor::Error, "Invalid token lifetime requested: #{lifetime}"
    end

    token_options[:expires_in] = duration
    token = Aptible::Auth::Token.create(token_options)
  rescue OAuth2::Error => e
    if e.code == 'otp_token_required'
      token_options[:otp_token] = options[:otp_token] ||
                                  ask('2FA Token: ')
      retry
    end

    raise Thor::Error, 'Could not authenticate with given credentials: ' \
                       "#{e.code}"
  end

  save_token(token.access_token)
  puts "Token written to #{token_file}"

  lifetime_format = { units: 2, joiner: ', ' }
  token_lifetime = (token.expires_at - token.created_at).round
  expires_in = ChronicDuration.output(token_lifetime, lifetime_format)
  puts "This token will expire after #{expires_in} " \
       '(use --lifetime to customize)'

  # Select a default program
  set_default_program
end
version() click to toggle source
# File lib/comply/cli/agent.rb, line 30
def version
  puts version_string
end

Private Instance Methods

version_string() click to toggle source
# File lib/comply/cli/agent.rb, line 113
def version_string
  bits = [
    'comply-cli',
    "v#{Comply::CLI::VERSION}"
  ]
  bits.join ' '
end