class AwsSessionToken::Options

Options class to define properties for the command line.

Constants

DURATION
SESSION_PROFILE

Attributes

console[RW]
credentials_file[RW]
duration[RW]
profile[RW]
profile_provided[RW]
session_profile[RW]
token[RW]
user[RW]

Public Class Methods

new() click to toggle source
# File lib/aws_session_token/options.rb, line 31
def initialize
  creds = Aws::SharedCredentials.new
  self.credentials_file = creds.path
  self.profile = creds.profile_name
  self.duration = DURATION
  self.profile_provided = false
  self.console = false
end

Public Instance Methods

parse(args) click to toggle source
# File lib/aws_session_token/options.rb, line 40
def parse(args)
  define_options.parse!(args)
  validate
end

Private Instance Methods

common_options(opts) click to toggle source
# File lib/aws_session_token/options.rb, line 113
def common_options(opts)
  opts.separator('')
  opts.separator('Common options:')
  opts.on_tail('-h', '--help', 'Show this message.') do
    puts opts
    exit
  end
  opts.on_tail('-v', '--version', 'Show version.') do
    puts SemVer.find.format(+ '%M.%m.%p%s')
    exit
  end
end
console_option(opts) click to toggle source
# File lib/aws_session_token/options.rb, line 92
def console_option(opts)
  opts.on('-c', '--console',
          'Output session information to the console as environment variables available to export.') do
    self.console = true
  end
end
define_options() click to toggle source
# File lib/aws_session_token/options.rb, line 47
def define_options
  opts = OptionParser.new
  opts.banner = 'Usage: aws_session_token [options]'
  opts.separator('')

  # Additional options
  file_option(opts)
  user_option(opts)
  profile_option(opts)
  session_profile_option(opts)
  console_option(opts)
  duration_option(opts)
  token_option(opts)
  common_options(opts)
  opts
end
duration_option(opts) click to toggle source
# File lib/aws_session_token/options.rb, line 99
def duration_option(opts)
  opts.on('-d', '--duration DURATION', Integer,
          'Specify the duration the of the token in seconds. (Default 3600)') do |d|
    self.duration = d
  end
end
file_option(opts) click to toggle source
# File lib/aws_session_token/options.rb, line 64
def file_option(opts)
  opts.on('-f', '--file FILE', 'Specify a custom credentials file.') do |f|
    self.credentials_file = f
  end
end
profile_option(opts) click to toggle source
# File lib/aws_session_token/options.rb, line 77
def profile_option(opts)
  opts.on('-p', '--profile PROFILE',
          'Specify the AWS credentials profile to use. Also sets user, if user is not provided.') do |p|
    self.profile = p
    self.profile_provided = true
  end
end
session_profile_option(opts) click to toggle source
# File lib/aws_session_token/options.rb, line 85
def session_profile_option(opts)
  opts.on('-s', '--session [SESSION_PROFILE]',
          'Specify the name of the profile used to store the session credentials.') do |s|
    self.session_profile = s || SESSION_PROFILE
  end
end
token_option(opts) click to toggle source
# File lib/aws_session_token/options.rb, line 106
def token_option(opts)
  opts.on('-t', '--token [TOKEN]',
          'Specify the OTP Token to use for creating the session credentials.') do |t|
    self.token = t
  end
end
user_option(opts) click to toggle source
# File lib/aws_session_token/options.rb, line 70
def user_option(opts)
  opts.on('-u', '--user USER',
          'Specify the AWS User name for passing to API.') do |u|
    self.user = u
  end
end
validate() click to toggle source
# File lib/aws_session_token/options.rb, line 126
def validate
  validate_profiles
  validate_output
end
validate_output() click to toggle source
# File lib/aws_session_token/options.rb, line 136
def validate_output
  raise ArgumentError, 'Either Console or Session Profile is required.' unless console || session_profile
end
validate_profiles() click to toggle source
# File lib/aws_session_token/options.rb, line 131
def validate_profiles
  raise ArgumentError, 'Profile and Session Profile must be different.' if profile == session_profile
  self.user ||= profile if profile_provided
end