Module: S3Ranger::CLI

Defined in:
lib/s3ranger/cli.rb

Defined Under Namespace

Classes: CreateBucket, Delete, DeleteBucket, Get, List, ListBuckets, Put, Sync, Url

Constant Summary collapse

AVAILABLE_ACLS =
[:public_read, :public_read_write, :private]
AVAILABLE_METHODS =
['read', 'get', 'put', 'write', 'delete']

Class Method Summary collapse

Class Method Details

.run(conf) ⇒ Object


273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/s3ranger/cli.rb', line 273

def run conf
  cmd = CmdParse::CommandParser.new true
  cmd.program_version = S3Ranger::VERSION

  cmd.options = CmdParse::OptionParserWrapper.new do |opt|
    opt.separator "Global options:"
  end

  cmd.main_command.short_desc = 'Tool belt for managing your S3 buckets'
  cmd.main_command.description = [] \
    << "Below you have a list of commands will allow you to manage your content" \
    << "stored in S3 buckets. For more information on each command, you can always" \
    << "use the `--help' parameter, just like this:" \
    << "" \
    << "   $ #{$0} sync --help" \

  # Commands used more often
  cmd.add_command List.new
  cmd.add_command Delete.new
  cmd.add_command Url.new
  cmd.add_command Put.new
  cmd.add_command Get.new
  cmd.add_command Sync.new

  # Bucket related options
  cmd.add_command ListBuckets.new
  cmd.add_command CreateBucket.new
  cmd.add_command DeleteBucket.new


  # Built-in commands
  cmd.add_command CmdParse::HelpCommand.new
  cmd.add_command CmdParse::VersionCommand.new

  # Defining the `execute` method as a closure, so we can forward the
  # arguments needed to run the instance of the chosen command.
  CmdParse::Command.class_eval do
    define_method :execute, lambda { |args|

      # Connecting to amazon
      s3 = AWS::S3.new(
        :access_key_id => conf[:AWS_ACCESS_KEY_ID],
        :secret_access_key => conf[:AWS_SECRET_ACCESS_KEY],
      )

      # From the command line
      key, file = args

      # Parsing the bucket name
      bucket = nil
      bucket, key = key.split(':') if key

      # Running our custom method inside of the command class, taking care
      # of the common errors here, saving duplications in each command;
      begin
        run s3, bucket, key, file, args
      rescue AWS::S3::Errors::AccessDenied
        raise FailureFeedback.new("Access Denied")
      rescue AWS::S3::Errors::NoSuchBucket
        raise FailureFeedback.new("There's no bucket named `#{bucket}'")
      rescue AWS::S3::Errors::NoSuchKey
        raise FailureFeedback.new("There's no key named `#{key}' in the bucket `#{bucket}'")
      end
    }
  end

  cmd.parse
end