Class: S3Ranger::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/s3ranger/cmd.rb

Instance Method Summary collapse

Constructor Details

#initialize(conf = conf) ⇒ Cmd

Returns a new instance of Cmd.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/s3ranger/cmd.rb', line 19

def initialize(conf = conf)
  # The chain that initializes our command and find the right action
  options, command, bucket, key, file = read_info_from_args(parse_args())

  # Finding the right command to run
  (cmd = find_cmd(command)) || (raise WrongUsage.new(nil, "Command `#{command}' does not exist"))

  # Now that we're sure we have things to do, we need to connect to amazon
  s3 = AWS::S3.new(
    :access_key_id => conf[:AWS_ACCESS_KEY_ID],
    :secret_access_key => conf[:AWS_SECRET_ACCESS_KEY],
  )

  # Calling the actuall command
  cmd.call({
    :options => options,
    :s3 => s3,
    :bucket => bucket,
    :key => key,
    :file => file,
  })
end

Instance Method Details

#find_cmd(name) ⇒ Object



42
43
44
45
46
# File 'lib/s3ranger/cmd.rb', line 42

def find_cmd name
  sym = "_cmd_#{name}".to_sym
  return nil unless Commands.public_methods.include? sym
  return Commands.method sym
end

#parse_argsObject

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/s3ranger/cmd.rb', line 48

def parse_args
  options = Hash.new

  args = [
    ['--help',       '-h', GetoptLong::NO_ARGUMENT],
    ['--force',      '-f', GetoptLong::NO_ARGUMENT],
    ['--acl',        '-a', GetoptLong::REQUIRED_ARGUMENT],
    ['--method',     '-m', GetoptLong::REQUIRED_ARGUMENT],
    ['--exclude',    '-e', GetoptLong::REQUIRED_ARGUMENT],
    ['--keep',       '-k', GetoptLong::NO_ARGUMENT],
    ['--dry-run',    '-d', GetoptLong::NO_ARGUMENT],
    ['--verbose',    '-v', GetoptLong::NO_ARGUMENT],
    ['--no-ssl',           GetoptLong::NO_ARGUMENT],
    ['--expires-in',       GetoptLong::REQUIRED_ARGUMENT],
  ]

  begin
    GetoptLong.new(*args).each {|opt, arg| options[opt] = (arg || true)}
  rescue StandardError => exc
    raise WrongUsage.new nil, exc.message
  end

  # Let's just show the help to the user
  raise WrongUsage.new(0, nil) if options['--help']

  # Returning the options to the next level
  options
end

#read_info_from_args(options) ⇒ Object

Raises:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/s3ranger/cmd.rb', line 77

def read_info_from_args(options)
  # Parsing expre date
  if options['--expires-in'] =~ /d|h|m|s/

    val = 0

    options['--expires-in'].scan(/(\d+\w)/) do |track|
      _, num, what = /(\d+)(\w)/.match(track[0]).to_a
      num = num.to_i

      case what
      when "d"; val += num * 86400
      when "h"; val += num * 3600
      when "m"; val += num * 60
      when "s"; val += num
      end
    end

    options['--expires-in'] = val
  end

  # Reading what to do from the user
  command = ARGV.shift
  raise WrongUsage.new(nil, "Need a command (eg.: `list', `listbuckets', etc)") if not command

  key, file = ARGV

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

  # Returning things we need in the next level
  [options, command, bucket, key, file]
end