Class: Comply::CLI::Agent

Inherits:
Thor
  • Object
show all
Includes:
Helpers::Program, Helpers::Token, Subcommands::Integrations, Thor::Actions
Defined in:
lib/comply/cli/agent.rb

Constant Summary

Constants included from Helpers::Program

Helpers::Program::PROGRAM_ENV_VAR

Constants included from Helpers::Token

Helpers::Token::TOKEN_ENV_VAR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Subcommands::Integrations

included

Methods included from Helpers::Integration

#env_vars_by_prompt, #prettify_integration, #pretty_print_integration, #prompt_and_create_integration, #prompt_and_update_integration, #prompt_for_env

Methods included from Helpers::Program

#accessible_programs, #current_program_id_hash, #default_program, #fetch_program_id, #pretty_print_program, #program_id_file, #save_program_id, #set_default_program

Methods included from Helpers::Token

#current_token_hash, #current_user_email, #fetch_token, #save_token, #token_file

Constructor Details

#initializeAgent

Returns a new instance of Agent.



24
25
26
27
# File 'lib/comply/cli/agent.rb', line 24

def initialize(*)
  Aptible::Resource.configure { |conf| conf.user_agent = version_string }
  super
end

Class Method Details

.exit_on_failure?Boolean

Forward return codes on failures.

Returns:

  • (Boolean)


20
21
22
# File 'lib/comply/cli/agent.rb', line 20

def self.exit_on_failure?
  true
end

Instance Method Details

#loginObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/comply/cli/agent.rb', line 40

def 
  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

#versionObject



30
31
32
# File 'lib/comply/cli/agent.rb', line 30

def version
  puts version_string
end