Module: S3Ranger::CLI

Defined in:
lib/s3ranger/cli.rb

Defined Under Namespace

Classes: BaseCmd, 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



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/s3ranger/cli.rb', line 393

def run conf
  cmd = CmdParse::CommandParser.new true
  cmd.program_name = File.basename $0
  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 =<<END.strip
S3Ranger provides a list of commands that will allow you to manage your content
stored in S3 buckets. To learn about each feature, please use the `help`
command:

$ #{File.basename $0} help sync"
END
  # 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}'")
      rescue AWS::S3::Errors::Base => exc
        raise FailureFeedback.new("Error: `#{exc.message}'")
      end
    }
  end

  cmd.parse
end