module S3Ranger::CLI

Constants

AVAILABLE_ACLS
AVAILABLE_METHODS

Public Class Methods

run(conf) click to toggle source
# 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

Private Instance Methods

run(conf) click to toggle source
# 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