s3cmd-dsl 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b8c1643ca59f650067f770ac0a6a7f2a22168dac
4
+ data.tar.gz: a7cf4bda02c43fc035ebdef76344dd0112cd99ea
5
+ SHA512:
6
+ metadata.gz: 18c467c374dabc608af61732dc0768a6ffe2d9f4f18a5f823c09611e3384ed39aec3ba9d2ae862df64caae5f1d9787b07d666c5ca1e49b604d3eb9eac1396749
7
+ data.tar.gz: 38ad08f1ba8b344e84c04c20f1c7f73cfb437abfb8dc881e96c1677da1b8267e83a4744929ad723ac19e80c63307e319aca693e8f16c25fbb02e02172bfb80fc
@@ -0,0 +1,190 @@
1
+ require_relative 'dsl_commands'
2
+ require_relative 'string'
3
+
4
+ module BaseCommands
5
+ include DSLCommands
6
+
7
+ class BaseDSLCommands
8
+ def path(path)
9
+ @commands[:path] = path
10
+ self
11
+ end
12
+ alias :path_to_s3cmd :path
13
+
14
+ def send_block_command
15
+ @send_block_command = if @commands.include? :path
16
+ @commands[:path]
17
+ @commands.delete(:path)
18
+ else
19
+ @path.empty? ? 's3cmd' : @path
20
+ end
21
+
22
+ @commands.each do |key, value|
23
+ @send_block_command += " #{key} '#{value}'"
24
+ end
25
+
26
+ @send_block_command.gsub!(/\'/, '').strip!
27
+ `#{@send_block_command}` if @execute
28
+
29
+ if @save
30
+ File.open(@save, 'w') do |file|
31
+ file.write(
32
+ <<-EOF.unindent_heredoc
33
+ #!/usr/bin/env bash
34
+ ##{@name}
35
+ ##{Time.now}
36
+ #{@send_block_command}
37
+ EOF
38
+ )
39
+ end
40
+ end
41
+
42
+ @send_block_command
43
+ end
44
+
45
+ def save_script(filename)
46
+ @save = filename
47
+ self
48
+ end
49
+
50
+ def execute
51
+ @execute = true
52
+ self
53
+ end
54
+ end
55
+
56
+ # Make bucket
57
+ def mb(bucket = nil) # s3://BUCKET
58
+ @commands[:mb] = bucket || @bucket
59
+ self
60
+ end
61
+ alias :create_new_bucket :mb
62
+ alias :make_bucket :mb
63
+ alias :mkdir :mb
64
+ alias :create_bucket :mb
65
+
66
+ # Remove bucket
67
+ def rb(bucket = nil) # s3://BUCKET
68
+ @commands[:rb] = bucket || @bucket
69
+ self
70
+ end
71
+ alias :remove_bucket :rb
72
+ alias :rmdir :rb
73
+ alias :delete_bucket :rb
74
+
75
+ # List objects or buckets
76
+ def ls(bucket = nil) # s3://BUCKET[/PREFIX]]
77
+ @commands[:ls] = bucket || @bucket
78
+ self
79
+ end
80
+ alias :list_bucket :ls
81
+ alias :list :ls
82
+
83
+ # List all object in all buckets
84
+ def la
85
+ @commands[:la] = ''
86
+ self
87
+ end
88
+ alias :list_all :la
89
+ alias :list_all_objects :la
90
+
91
+ # Put file into bucket (i.e. upload to S3)
92
+ def put(files = nil, bucket = nil) # FILE [FILE...] s3://BUCKET[/PREFIX]
93
+ put_files = @files || files
94
+ put_bucket = @bucket || bucket
95
+
96
+ @commands[:put] = "#{put_files} #{put_bucket}"
97
+ self
98
+ end
99
+ alias :upload :put
100
+
101
+ # Get file from bucket (i.e. download from S3)
102
+ def get(object = nil, files = nil) # s3://BUCKET/OBJECT LOCAL_FILE
103
+ get_files = @files || files
104
+ get_object = @bucket || object
105
+
106
+ @commands[:get] = "#{get_object} #{get_files}"
107
+ self
108
+ end
109
+ alias :download :get
110
+
111
+ # Delete file from bucket
112
+ def del(object = nil) # s3://BUCKET/OBJECT
113
+ @commands[:del] = @bucket || object
114
+ self
115
+ end
116
+ alias :delete :del
117
+ alias :rm :del
118
+ alias :delete_object :del
119
+ alias :delete_file :del
120
+ alias :remove :del
121
+
122
+ # Backup a directory tree to S3
123
+ # Restore a tree from S3 to local directory
124
+ def sync(src_object, dest_object) # LOCAL_DIR s3://BUCKET[/PREFIX] or s3://BUCKET[/PREFIX] LOCAL_DIR
125
+ @commands[:sync] = "#{src_object} #{dest_object}"
126
+ self
127
+ end
128
+ alias :sync_directory :sync
129
+ alias :sync_dir :sync
130
+ alias :synchronize :sync
131
+
132
+ # Make a copy of a file (cp) or move a file (mv). Destination can be in the same bucket with a dif‐
133
+ # ferent name or in another bucket with the same or different name. Adding --acl-public will make the
134
+ # destination object publicly accessible (see below).
135
+ def cp(src_bucket, dest_bucket) # s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]
136
+ @commands[:cp] = "#{src_bucket} #{dest_bucket}"
137
+ self
138
+ end
139
+ alias :copy :cp
140
+
141
+ # Make a copy of a file (cp) or move a file (mv). Destination can be in the same bucket with a dif‐
142
+ # ferent name or in another bucket with the same or different name. Adding --acl-public will make the
143
+ # destination object publicly accessible (see below).
144
+ def mv(src_bucket, dest_bucket) # s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]
145
+ @commands[:mv] = "#{src_bucket} #{dest_bucket}"
146
+ self
147
+ end
148
+ alias :move :mv
149
+
150
+ # Modify Access control list for Bucket or Files. Use with --acl-public or --acl-private
151
+ def setacl(bucket = nil) # s3://BUCKET[/OBJECT]
152
+ @commands[:setacl] = @bucket || bucket
153
+ self
154
+ end
155
+ alias :set_access_control_list :setacl
156
+
157
+ # Get various information about a Bucket or Object
158
+ def info(bucket = nil) # s3://BUCKET[/OBJECT]
159
+ @commands[:info] = @bucket || bucket
160
+ self
161
+ end
162
+
163
+ # Disk usage - amount of data stored in S3
164
+ def du(bucket = nil) # [s3://BUCKET[/PREFIX]]
165
+ @commands[:du] = @bucket || bucket
166
+ self
167
+ end
168
+ alias :disk_usage :du
169
+
170
+ # Enable/disable bucket access logging
171
+ def accesslog(bucket = nil)
172
+ @commands[:accesslog] = @bucket || bucket
173
+ self
174
+ end
175
+ alias :logging :accesslog
176
+
177
+ # Sign arbitrary string using the secret key
178
+ def sign(string)
179
+ @commands[:sign] = string
180
+ self
181
+ end
182
+ alias :secretkey :sign
183
+
184
+ # Fix invalid file names in a bucket
185
+ def fixbucket(bucket = nil)
186
+ @commands[:fixbucket] = @bucket || bucket
187
+ self
188
+ end
189
+ alias :fix :fixbucket
190
+ end
@@ -0,0 +1,42 @@
1
+ module CloudFrontCommands
2
+ # Commands for CloudFront management
3
+ # List CloudFront distribution points
4
+ def cflist
5
+ @commands[:cflist] = ''
6
+ self
7
+ end
8
+ alias :cloudfront_list :cflist
9
+ alias :cloudfront_ls :cflist
10
+ alias :cfls :cflist
11
+
12
+ # Display CloudFront distribution point parameters
13
+ def cfinfo(dist_id) # [cf://DIST_ID]
14
+ @commands[:cfinfo] = dist_id
15
+ self
16
+ end
17
+ alias :cloudfront_info :cfinfo
18
+
19
+ # Create CloudFront distribution point
20
+ def cfcreate(bucket) # s3://BUCKET
21
+ @commands[:cfcreate] = bucket
22
+ self
23
+ end
24
+ alias :cloudfront_create :cfcreate
25
+ alias :cloudfront_make :cfcreate
26
+ alias :cfmake :cfcreate
27
+ alias :cfmk :cfcreate
28
+
29
+ # Delete CloudFront distribution point
30
+ def cfdelete(dist_id) # cf://DIST_ID
31
+ @commands[:cfdelete] = dist_id
32
+ self
33
+ end
34
+ alias :cloudfront_delete :cfdelete
35
+
36
+ # Change CloudFront distribution point parameters
37
+ def cfmodify(dist_id) # cf://DIST_ID
38
+ @commands[:cfmodify] = dist_id
39
+ self
40
+ end
41
+ alias :cloudfront_modify :cfmodify
42
+ end
@@ -0,0 +1,16 @@
1
+ module DSLCommands
2
+ def bucket(bucket)
3
+ @bucket = bucket
4
+ self
5
+ end
6
+ alias :remote_bucket :bucket
7
+ alias :object :bucket
8
+ alias :remote_file :bucket
9
+
10
+ def files(*files)
11
+ @files = files.join(' ')
12
+ self
13
+ end
14
+ alias :local_files :files
15
+ alias :file :files
16
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def unindent_heredoc
3
+ gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "")
4
+ end
5
+ end
data/lib/dsl/dsl.rb ADDED
@@ -0,0 +1,13 @@
1
+ require_relative 's3cmd'
2
+
3
+ module S3Cmd
4
+ def s3cmd(name = nil, command = nil, execute = nil, &block)
5
+ s3cmd = S3CmdDSL.new(name)
6
+ s3cmd.send(command) if command
7
+ s3cmd.execute if execute == 'execute'
8
+ if block_given? && command.nil?
9
+ s3cmd.instance_eval(&block)
10
+ end
11
+ s3cmd.send_block_command
12
+ end
13
+ end
data/lib/dsl/s3cmd.rb ADDED
@@ -0,0 +1,20 @@
1
+ require_relative 'commands/base_commands'
2
+ require_relative 'commands/cloudfront_commands'
3
+ require_relative 'dsl'
4
+
5
+ class S3CmdDSL < BaseCommands::BaseDSLCommands
6
+ include BaseCommands
7
+ include CloudFrontCommands
8
+
9
+ def initialize(name)
10
+ @name = name
11
+ @path = `which s3cmd`.strip
12
+ @commands = {}
13
+ end
14
+
15
+ def method_missing(method, *args)
16
+ meth = method.to_s.gsub("_", "-").gsub(/^/, "--")
17
+ @commands[meth] = args.join(', ')
18
+ self
19
+ end
20
+ end
data/lib/s3cmd-dsl.rb ADDED
@@ -0,0 +1,11 @@
1
+ # = s3cmd-dsl - A gem providing a ruby DSL interface to s3cmd Amazon S3 client.
2
+ #
3
+ # Homepage:: http://github.com/jjuliano/s3cmd-dsl
4
+ # Author:: Joel Bryan Juliano
5
+ # Copyright:: (cc) 2013 Joel Bryan Juliano
6
+ # License:: MIT
7
+ #
8
+ require_relative 'version'
9
+
10
+ Dir[File.join(File.dirname(__FILE__), 'dsl/**/*.rb')].sort.reverse.each { |lib| require lib }
11
+
data/lib/version.rb ADDED
@@ -0,0 +1,10 @@
1
+ module S3Cmd #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
10
+
data/test/test_dsl.rb ADDED
@@ -0,0 +1,210 @@
1
+ require_relative 'test_helper'
2
+ class TestS3CmdDSLGeneral < Test::Unit::TestCase
3
+ def test_with_params
4
+ test_class('test_with_params') {
5
+ s3cmd('Test With Parameters') do
6
+ path 'test-s3cmd'
7
+ mb 's3://BUCKET'
8
+ end
9
+ }
10
+
11
+ klass = TestClass.new
12
+ assert_equal('test-s3cmd mb s3://BUCKET', klass.test_with_params)
13
+ end
14
+
15
+ def test_without_params
16
+ test_class('test_without_params') {
17
+ s3cmd('Test Without Parameters') do
18
+ path 'test-s3cmd'
19
+ bucket 's3://BUCKET'
20
+ mb
21
+ end
22
+ }
23
+
24
+ klass = TestClass.new
25
+ assert_equal('test-s3cmd mb s3://BUCKET', klass.test_without_params)
26
+ end
27
+
28
+ def test_with_keywords
29
+ test_class('test_with_keywords') {
30
+ s3cmd('Test With Keywords') do
31
+ path 'test-s3cmd'
32
+ ls 's3://BUCKET'
33
+ rinclude '[0-9].*.jpg'
34
+ exclude '*.jpg'
35
+ end
36
+ }
37
+
38
+ klass = TestClass.new
39
+ assert_equal('test-s3cmd ls s3://BUCKET --rinclude [0-9].*.jpg --exclude *.jpg', klass.test_with_keywords)
40
+ end
41
+
42
+ def test_one_liner
43
+ test_class('test_one_liner') { s3cmd('Test One Liner', 'configure') }
44
+
45
+ klass = TestClass.new
46
+ s3cmd_path = `which s3cmd`.strip!
47
+ path = s3cmd_path.nil? ? 's3cmd' : s3cmd_path
48
+ assert_equal("#{path} --configure", klass.test_one_liner)
49
+ end
50
+
51
+ def test_chaining
52
+ test_class('test_chaining') {
53
+ s3cmd('Test Chaining') { path('test-s3cmd').ls('s3://BUCKET').rinclude('[0-9].*.jpg').exclude('*.jpg') }
54
+ }
55
+
56
+ klass = TestClass.new
57
+ assert_equal('test-s3cmd ls s3://BUCKET --rinclude [0-9].*.jpg --exclude *.jpg', klass.test_chaining)
58
+ end
59
+
60
+ def test_path
61
+ test_class('test_path') {
62
+ s3cmd('Test Path') { ls }
63
+ }
64
+
65
+ klass = TestClass.new
66
+ s3cmd_path = `which s3cmd`.strip!
67
+ path = s3cmd_path.nil? ? 's3cmd' : s3cmd_path
68
+ assert_equal("#{path} ls", klass.test_path)
69
+ end
70
+
71
+ def test_cloudfront_methods
72
+ test_class('test_cloudfront_methods') {
73
+ s3cmd('Test CloudFront Methods') { cflist }
74
+ }
75
+
76
+ klass = TestClass.new
77
+ s3cmd_path = `which s3cmd`.strip!
78
+ path = s3cmd_path.nil? ? 's3cmd' : s3cmd_path
79
+ assert_equal("#{path} cflist", klass.test_cloudfront_methods)
80
+ end
81
+
82
+ def test_files_parameter
83
+ test_class('test_files_parameter') {
84
+ filelist = ['file1', 'file2']
85
+ s3cmd('Test Files Parameter') do
86
+ path 'test-s3cmd'
87
+ files filelist
88
+ bucket 's3://BUCKET'
89
+ put
90
+ end
91
+ }
92
+
93
+ klass = TestClass.new
94
+ assert_equal("test-s3cmd put file1 file2 s3://BUCKET", klass.test_files_parameter)
95
+ end
96
+
97
+ COMMANDS = [:mb, :rb, :ls, :del, :setacl, :info, :du, :accesslog, :fixbucket]
98
+
99
+ # Test with Parameters
100
+ COMMANDS.each do |command|
101
+ meth = %Q{
102
+ def test_#{command.to_s}_with_params
103
+ test_class("test_#{command.to_s}_with_params") {
104
+ s3cmd("Test #{command.to_s} With Parameters") do
105
+ path 'test-s3cmd'
106
+ #{command.to_s} 's3://BUCKET'
107
+ end
108
+ }
109
+
110
+ klass = TestClass.new
111
+ assert_equal("test-s3cmd #{command.to_s} s3://BUCKET", klass.test_#{command.to_s}_with_params)
112
+ end
113
+ }
114
+ eval(meth)
115
+ end
116
+
117
+ # Test without parameters
118
+ COMMANDS.each do |command|
119
+ meth = %Q{
120
+ def test_#{command.to_s}_without_params
121
+ test_class("test_#{command.to_s}_without_params") {
122
+ s3cmd("Test #{command.to_s} Without Parameters") do
123
+ path 'test-s3cmd'
124
+ bucket 's3://BUCKET'
125
+ #{command.to_s}
126
+ end
127
+ }
128
+
129
+ klass = TestClass.new
130
+ assert_equal("test-s3cmd #{command.to_s} s3://BUCKET", klass.test_#{command.to_s}_without_params)
131
+ end
132
+ }
133
+ eval(meth)
134
+ end
135
+
136
+ # Test with parameters and passing arguments
137
+ COMMANDS.each do |command|
138
+ meth = %Q{
139
+ def test_#{command.to_s}_with_params_and_arguments
140
+ test_class("test_#{command.to_s}_with_params_and_arguments") {
141
+ s3cmd("Test #{command.to_s} With Parameters") do
142
+ path 'test-s3cmd'
143
+ #{command.to_s} 's3://BUCKET'
144
+ recursive
145
+ encoding 'utf-8'
146
+ end
147
+ }
148
+
149
+ klass = TestClass.new
150
+ assert_equal("test-s3cmd #{command.to_s} s3://BUCKET --recursive --encoding utf-8", klass.test_#{command.to_s}_with_params_and_arguments)
151
+ end
152
+ }
153
+ eval(meth)
154
+ end
155
+
156
+ # Test without parameters and passing arguments
157
+ COMMANDS.each do |command|
158
+ meth = %Q{
159
+ def test_#{command.to_s}_without_params_and_arguments
160
+ test_class("test_#{command.to_s}_without_params_and_arguments") {
161
+ s3cmd("Test #{command.to_s} Without Parameters") do
162
+ path 'test-s3cmd'
163
+ bucket 's3://BUCKET'
164
+ #{command.to_s}
165
+ recursive
166
+ encoding 'utf-8'
167
+ end
168
+ }
169
+
170
+ klass = TestClass.new
171
+ assert_equal("test-s3cmd #{command.to_s} s3://BUCKET --recursive --encoding utf-8", klass.test_#{command.to_s}_without_params_and_arguments)
172
+ end
173
+ }
174
+ eval(meth)
175
+ end
176
+
177
+ # Test chaining with parameters and passing arguments
178
+ COMMANDS.each do |command|
179
+ meth = %Q{
180
+ def test_chaining_#{command.to_s}_with_params_and_arguments
181
+ test_class("test_chaining_#{command.to_s}_with_params_and_arguments") {
182
+ s3cmd("Test #{command.to_s} With Parameters") do
183
+ path('test-s3cmd').#{command.to_s}('s3://BUCKET').recursive.encoding('utf-8')
184
+ end
185
+ }
186
+
187
+ klass = TestClass.new
188
+ assert_equal("test-s3cmd #{command.to_s} s3://BUCKET --recursive --encoding utf-8", klass.test_chaining_#{command.to_s}_with_params_and_arguments)
189
+ end
190
+ }
191
+ eval(meth)
192
+ end
193
+
194
+ # Test chaining without parameters and passing arguments
195
+ COMMANDS.each do |command|
196
+ meth = %Q{
197
+ def test_chaining_#{command.to_s}_without_params_and_arguments
198
+ test_class("test_chaining_#{command.to_s}_without_params_and_arguments") {
199
+ s3cmd("Test #{command.to_s} Without Parameters") do
200
+ path('test-s3cmd').bucket('s3://BUCKET').#{command.to_s}.recursive.encoding('utf-8')
201
+ end
202
+ }
203
+
204
+ klass = TestClass.new
205
+ assert_equal("test-s3cmd #{command.to_s} s3://BUCKET --recursive --encoding utf-8", klass.test_chaining_#{command.to_s}_without_params_and_arguments)
206
+ end
207
+ }
208
+ eval(meth)
209
+ end
210
+ end
@@ -0,0 +1,12 @@
1
+ require 'test/unit'
2
+ Dir[File.join(File.dirname(__FILE__), '/../lib/*.rb')].sort.reverse.each { |lib| require lib }
3
+
4
+ class TestClass; end
5
+ def test_class(method_name, &block)
6
+ TestClass.class_eval do
7
+ include S3Cmd
8
+ define_method(method_name) do
9
+ self.instance_eval(&block)
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3cmd-dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel Bryan Juliano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem providing a ruby DSL interface to s3cmd Amazon S3 client.
14
+ email: joelbryan.juliano@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/dsl/commands/base_commands.rb
20
+ - lib/dsl/commands/cloudfront_commands.rb
21
+ - lib/dsl/commands/dsl_commands.rb
22
+ - lib/dsl/commands/string.rb
23
+ - lib/dsl/dsl.rb
24
+ - lib/dsl/s3cmd.rb
25
+ - lib/s3cmd-dsl.rb
26
+ - lib/version.rb
27
+ - test/test_dsl.rb
28
+ - test/test_helper.rb
29
+ homepage: http://github.com/jjuliano/s3cmd-dsl
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.0.0
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: A gem providing a ruby DSL interface to s3cmd Amazon S3 client.
53
+ test_files:
54
+ - test/test_dsl.rb
55
+ - test/test_helper.rb