Module: AwsRunAs::Cli

Defined in:
lib/aws_runas/cli.rb

Class Method Summary collapse

Class Method Details

.load_opts(args: ARGV) ⇒ Object

loads the command-line options.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aws_runas/cli.rb', line 26

def load_opts(args: ARGV)
  Optimist.options(args) do
    version AwsRunAs::VERSION
    banner <<-EOS.gsub(/^ {10}/, '')
      aws-runas: Run commands under AWS IAM roles

      Usage:
        aws-runas [options] COMMAND ARGS

      If COMMAND is omitted, the default shell ($SHELL, /bin/sh, or cmd.exe,
      depending on your system) will launch.

      [options] are:
    EOS

    opt :no_role, 'Get a session token only, do not assume a role', type: TrueClass, default: nil
    opt :skip_prompt, 'Do not launch interactive sessions with the fancy prompt', type: TrueClass, default: nil
    opt :path, 'Path to the AWS config file', type: String
    opt :profile, 'The AWS profile to load', type: String, default: 'default'
    opt :duration, 'The duration in seconds for temporary credentials', type: Integer, default: 3600
    stop_on_unknown
  end
end

.read_mfa_if_needed(path: nil, profile: 'default') ⇒ Object

Reads the MFA code from standard input.



62
63
64
65
66
67
68
# File 'lib/aws_runas/cli.rb', line 62

def read_mfa_if_needed(path: nil, profile: 'default')
  @cfg = AwsRunAs::Config.new(path: path, profile: profile)
  return nil unless @cfg.mfa_required?
  STDOUT.print 'Enter MFA code: '
  STDOUT.flush
  STDIN.gets.chomp
end

.startObject

Start the CLI. Load options (profile, specific config path), and run main.



52
53
54
55
56
57
58
59
# File 'lib/aws_runas/cli.rb', line 52

def start
  opts = load_opts
  mfa_code = read_mfa_if_needed(path: opts[:path], profile: opts[:profile])
  @main = AwsRunAs::Main.new(path: opts[:path], profile: opts[:profile], mfa_code: mfa_code, no_role: opts[:no_role], duration_seconds: opts[:duration])
  @main.assume_role
  command = ARGV.shift
  @main.handoff(command: command, argv: ARGV, skip_prompt: opts[:skip_prompt])
end