Class: Amazon::WebServices::Util::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/webservices/util/command_line.rb

Constant Summary collapse

SDKS =
['Mechanical Turk']
MISSING_AUTH =
"You are missing authentication information required to utilize Amazon Web Services.  You will now be prompted for your <%= color('Access Key ID',BOLD) %> and your <%= color('Secret Access Key',BOLD) %>.  If you do not have this information, please log into http://www.amazonaws.com/  \n\n"
RESUME_AUTH =
"Authentication information has been initialized.  Continuing... \n\n"
MAIN_HELP =
<<EOF

This is the <%= color('RubyAWS', RED, BOLD) %> Command Line Application's Interactive mode.
You got here by typing:

  <%= color('$ ruby-aws',GREEN) %>

This application currently supports the following functionality:

<%= list( ['Amazon Web Services Authentication Configuration'] ) %>
You can also invoke this tool with commandline parameters.  For more information:

  <%= color('$ ruby-aws --help',GREEN) %>

Thanks for using Amazon Web Services!

EOF

Instance Method Summary collapse

Constructor Details

#initialize(interactive = true) ⇒ CommandLine

Returns a new instance of CommandLine.



37
38
39
40
41
42
43
44
# File 'lib/amazon/webservices/util/command_line.rb', line 37

def initialize( interactive = true )
  @interactive = interactive
  @h = HighLine.new
  @h.wrap_at = :auto
  @store = Amazon::Util::UserDataStore.new :AWS
  HighLine.use_color = useColor?
  HighLine.track_eof = false # disabling because it misbehaves
end

Instance Method Details

#authIdObject



70
71
72
# File 'lib/amazon/webservices/util/command_line.rb', line 70

def authId
  @store.get(:Auth,:AccessKeyId)
end

#authKeyObject



67
68
69
# File 'lib/amazon/webservices/util/command_line.rb', line 67

def authKey
  @store.get(:Auth,:SecretAccessKey)
end

#checkAuthConfigObject



46
47
48
49
50
51
52
# File 'lib/amazon/webservices/util/command_line.rb', line 46

def checkAuthConfig
  if @store.get(:Auth,:AccessKeyId).nil? or @store.get(:Auth,:SecretAccessKey).nil?
    @h.say MISSING_AUTH
    getAuthConfig
    @h.say RESUME_AUTH
  end
end

#default_menuObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/amazon/webservices/util/command_line.rb', line 92

def default_menu
  loop do
    @h.choose do |menu|
      menu.header = "\n" + @h.color('RubyAWS',HighLine::BOLD,HighLine::RED) + " " + @h.color('Command Line Application',HighLine::BOLD) + " " + @h.color('[Interactive Mode]',HighLine::GREEN,HighLine::BOLD)
      menu.select_by = :index
      menu.choice( 'Configure Amazon Web Services Authentication' ) do
        if @h.agree( "\nCurrent ID: #{@h.color(authId,HighLine::BOLD)}\nCurrent Key: #{@h.color(authKey,HighLine::BOLD)}\nDo you want to change?" )
          getAuthConfig
        end
      end
      menu.choice( 'Toggle Color' ) { toggleColor }
      menu.choice( :help ) do
        @h.say MAIN_HELP
      end
      menu.choice( 'Save and Quit' ) { @store.save ; exit }
      menu.prompt = "\nWhat would you like to do? "
    end
  end
end

#getAuthConfig(default = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/amazon/webservices/util/command_line.rb', line 54

def getAuthConfig( default={} )
  id = @h.ask( 'What is your Access Key ID?' ) {|q| q.validate = /^[A-Z0-9]{20}$/ ; q.default = authId.to_s ; q.first_answer = default[:ID] }
  key = @h.ask( 'What is your Secret Access Key?' ) {|q| q.validate = /^[\w\/+]{40}$/ ; q.default = authKey.to_s ; q.first_answer = default[:Key] }

  @store.set(:Auth,:AccessKeyId,id)
  @store.set(:Auth,:SecretAccessKey,key)

  should_save = @h.agree( 'Would you like to save your authentication information?' )
  @store.save if should_save
rescue
  raise "Unable to retrieve authentication information from the Console"
end

#parseOptionsObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/amazon/webservices/util/command_line.rb', line 112

def parseOptions
  res = {}
  opts = OptionParser.new

  opts.on( '-i', '--interactive', 'Load Interactive Mode' ) { res[:Interactive] = true }
  opts.on( '-a', '--authenticate', 'Configure Authentication Options' ) { res[:Auth] = true }
  opts.on( '--id=ID', 'Set Access Key ID (requires "-a")' ) { |id| res[:ID] = id }
  opts.on( '--key=KEY', 'Set Secret Access Key (requires "-a")' ) { |key| res[:Key] = key }

  begin
    opts.parse(ARGV)
    raise "-i and -a are exclusive options.  Please pick one." if res[:Interactive] and res[:Auth]
    raise "--id requires -a" if res[:ID] and !res[:Auth]
    raise "--key requires -a" if res[:Key] and !res[:Auth]
    res[:Mode] = res[:Auth] ? :Auth : :Interactive
  rescue => e
    p e.message
  end
  res
end

#runObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/amazon/webservices/util/command_line.rb', line 133

def run

  opts = parseOptions

  case opts[:Mode]
  when :Interactive

    @h.say "\n<%= color('Welcome to',BOLD) %> <%= color('RubyAWS #{RubyAWS::VERSION}',BOLD,RED) %>"
    @h.say "This version includes SDK extensions for: <%= list (#{SDKS.inspect}.collect {|a| color(a,BOLD)}), :inline, ' and ' %>\n\n"

    checkAuthConfig

    default_menu
  when :Auth

    getAuthConfig( :Key => opts[:Key], :ID => opts[:ID] )

  end
end

#toggleColorObject



86
87
88
89
90
# File 'lib/amazon/webservices/util/command_line.rb', line 86

def toggleColor
  value = !useColor?
  @store.set(:Misc,:ColorTerminal,value)
  HighLine.use_color = value
end

#useColor?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/amazon/webservices/util/command_line.rb', line 74

def useColor?
  if @store.get(:Misc,:ColorTerminal).nil?
    if @interactive and @h.agree( "Should the console application use color? (y/n)" )
      @store.set(:Misc,:ColorTerminal,true)
    else
      @store.set(:Misc,:ColorTerminal,false)
    end
    @store.save
  end
  return @store.get(:Misc,:ColorTerminal)
end