| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
$:.unshift(File.join(File.dirname(File.dirname(__FILE__)), 'lib')) |
| 17 |
|
| 18 |
require 'rubygems' |
| 19 |
require 'optparse' |
| 20 |
require 'logger' |
| 21 |
|
| 22 |
def env_bool key, default |
| 23 |
if ENV.key?(key) |
| 24 |
!['0', 'false', 'no', 'off'].include?(ENV[key].downcase) |
| 25 |
else |
| 26 |
default |
| 27 |
end |
| 28 |
end |
| 29 |
|
| 30 |
|
| 31 |
options = { |
| 32 |
:repl => env_bool('AWSRB', nil), |
| 33 |
:log => env_bool('AWSRB_LOG', true), |
| 34 |
:color => env_bool('AWSRB_COLOR', true), |
| 35 |
:debug => env_bool('AWSRB_DEBUG', false), |
| 36 |
:load_paths => [], |
| 37 |
:require => [], |
| 38 |
:execute => [], |
| 39 |
} |
| 40 |
|
| 41 |
OptionParser.new do |opts| |
| 42 |
|
| 43 |
opts.banner = "Usage: aws-rb [options]" |
| 44 |
|
| 45 |
opts.on("--repl REPL", "specify the repl environment, pry or irb") do |value| |
| 46 |
options[:repl] = value |
| 47 |
end |
| 48 |
|
| 49 |
opts.on("-e 'command'", "one line of script. Several -e's allowed.") do |value| |
| 50 |
options[:execute] << value |
| 51 |
options[:log] = false unless options[:log_set] |
| 52 |
options[:debug] = false unless options[:debug_set] |
| 53 |
end |
| 54 |
|
| 55 |
opts.on("-l", "--[no-]log", "log client requets, on by default") do |value| |
| 56 |
options[:log] = value |
| 57 |
options[:log_set] = true |
| 58 |
end |
| 59 |
|
| 60 |
opts.on("-c", "--[no-]color", "colorize request logging, on by default") do |value| |
| 61 |
options[:color] = value |
| 62 |
end |
| 63 |
|
| 64 |
opts.on("--profile PROFILE", "Use a specific profile from your credential file.") do |value| |
| 65 |
options[:profile] = value |
| 66 |
end |
| 67 |
|
| 68 |
opts.on("-d", "--[no-]debug", "log HTTP wire traces, off by default") do |value| |
| 69 |
options[:debug] = value |
| 70 |
options[:debug_set] = true |
| 71 |
end |
| 72 |
|
| 73 |
opts.on("-Idirectory", Array, "specify $LOAD_PATH directory (may be used more than once)") do |values| |
| 74 |
options[:load_paths] += values |
| 75 |
end |
| 76 |
|
| 77 |
opts.on("-rlibrary", Array, "require the library") do |values| |
| 78 |
options[:require] += values |
| 79 |
end |
| 80 |
|
| 81 |
opts.on("-v", "--verbose", "enable client logging and HTTP wire tracing") do |value| |
| 82 |
options[:log] = true |
| 83 |
options[:log_set] = true |
| 84 |
options[:debug] = true |
| 85 |
options[:debug_set] = true |
| 86 |
end |
| 87 |
|
| 88 |
opts.on("-q", "--quiet", "disable client logging and HTTP wire tracing") do |value| |
| 89 |
options[:log] = false |
| 90 |
options[:log_set] = true |
| 91 |
options[:debug] = false |
| 92 |
options[:debug_set] = true |
| 93 |
end |
| 94 |
|
| 95 |
opts.on("-h", "--help") do |
| 96 |
puts opts |
| 97 |
exit |
| 98 |
end |
| 99 |
|
| 100 |
end.parse! |
| 101 |
|
| 102 |
|
| 103 |
options[:load_paths].each do |path| |
| 104 |
$LOAD_PATH.unshift(path) |
| 105 |
end |
| 106 |
|
| 107 |
require 'aws-sdk-v1' |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
cfg = {} |
| 112 |
|
| 113 |
if options[:profile] |
| 114 |
provider = AWS::Core::CredentialProviders::SharedCredentialFileProvider.new( |
| 115 |
:profile_name => options[:profile] |
| 116 |
) |
| 117 |
cfg[:credential_provider] = provider |
| 118 |
end |
| 119 |
|
| 120 |
if options[:log] |
| 121 |
logger = Logger.new($stdout) |
| 122 |
logger.formatter = proc {|severity, datetime, progname, msg| msg } |
| 123 |
cfg[:logger] = logger |
| 124 |
end |
| 125 |
|
| 126 |
if options[:color] |
| 127 |
cfg[:log_formatter] = AWS::Core::LogFormatter.colored |
| 128 |
end |
| 129 |
|
| 130 |
if options[:debug] |
| 131 |
cfg[:http_wire_trace] = true |
| 132 |
end |
| 133 |
|
| 134 |
AWS.config(cfg) |
| 135 |
|
| 136 |
options[:require].each do |library| |
| 137 |
require(library) |
| 138 |
end |
| 139 |
|
| 140 |
unless options[:execute].empty? |
| 141 |
eval(options[:execute].join("\n")) |
| 142 |
exit |
| 143 |
end |
| 144 |
|
| 145 |
unless options[:repl] == 'irb' |
| 146 |
|
| 147 |
|
| 148 |
require 'nokogiri' |
| 149 |
require 'json' |
| 150 |
end |
| 151 |
|
| 152 |
class PryNotAvailable < StandardError; end |
| 153 |
|
| 154 |
def run_with_pry |
| 155 |
begin |
| 156 |
require 'pry' |
| 157 |
rescue LoadError |
| 158 |
raise PryNotAvailable |
| 159 |
end |
| 160 |
Pry.config.prompt = [proc { "AWS> " }, proc { "AWS| " }] |
| 161 |
AWS.pry |
| 162 |
end |
| 163 |
|
| 164 |
def run_with_irb |
| 165 |
require 'irb' |
| 166 |
IRB.start |
| 167 |
end |
| 168 |
|
| 169 |
case options[:repl] |
| 170 |
when 'pry' then run_with_pry |
| 171 |
when 'irb' then run_with_irb |
| 172 |
else |
| 173 |
begin |
| 174 |
run_with_pry |
| 175 |
rescue PryNotAvailable |
| 176 |
warn("Pry not available, falling back to irb") |
| 177 |
run_with_irb |
| 178 |
end |
| 179 |
end |