s3diff 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: 9214fc3b3d1057f64f54293b48168c5df0edf2c5
4
+ data.tar.gz: 0464c764900078951eca13de55458dc636bc7bc7
5
+ SHA512:
6
+ metadata.gz: d9c6e195752d3e88af9b7d63c939a7f21c5254021f7da052af69e1ba1a7a3d1edcad16488e89fd2c9051019d6e1ca22ca9a06640ccb3728b692b25e4ac2943f0
7
+ data.tar.gz: 7136697d46c01d2796333d70b5019ca2565476095d50494345b9bdf2173d8505e4ffd53a565394e16d8102faf61af57cafd2d262c5672c84d94af0425c846032
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in s3diff.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 masa21kik
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # S3diff
2
+
3
+ Show difference between S3 objects and local files.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 's3diff'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install s3diff
20
+
21
+ ## Usage
22
+
23
+ $ s3diff s3://example-bucket/foo.txt ./foo.txt
24
+ foo
25
+ - bar
26
+ + baz
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test-unit` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/masa21kik/s3diff.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "s3diff"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/s3diff ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "s3diff"
4
+ require "s3diff/cli"
5
+
6
+ S3diff::Cli.new.run(ARGV)
data/lib/s3diff/cli.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "optparse"
2
+
3
+ module S3diff
4
+ class Cli
5
+ def run(args)
6
+ parse_args(args)
7
+ comp = Comparator.new(@files[0], @files[1])
8
+ $stdout.puts comp.diff.to_s(@options[:format]) unless comp.same?
9
+ end
10
+
11
+ private
12
+
13
+ def parse_args(args)
14
+ @options = { format: $stdout.tty? ? :color : :text }
15
+ opt = OptionParser.new
16
+ opt.banner = "Usage: #{$PROGRAM_NAME} [options] file1 file2"
17
+ opt.on("-f", "--format FORMAT", "Diffy format [default=color]") { |v| @options[:format] = v.to_sym }
18
+ opt.on("-p", "--aws-profile PROFILE") { |v| ENV["AWS_PROFILE"] = v }
19
+ @files = opt.parse(args)
20
+ abort(opt.help) if @files.size != 2
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ require "diffy"
2
+
3
+ module S3diff
4
+ class Comparator
5
+ def initialize(file1, file2, s3_options = {})
6
+ @s3_options = s3_options
7
+ @file1 = file_object(file1)
8
+ @file2 = file_object(file2)
9
+ end
10
+
11
+ def same_etag?
12
+ @file1.etag == @file2.etag
13
+ end
14
+
15
+ def same?
16
+ same_etag? || diff.to_s.empty?
17
+ end
18
+
19
+ def diff
20
+ @diff ||= Diffy::Diff.new(@file1.path, @file2.path, source: "files")
21
+ end
22
+
23
+ private
24
+
25
+ def file_object(file)
26
+ uri = URI.parse(file)
27
+ case uri.scheme
28
+ when "s3" then S3File.new(file, s3_client)
29
+ else LocalFile.new(file)
30
+ end
31
+ end
32
+
33
+ def s3_client
34
+ @s3c ||= S3Client.new(@s3_options)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,21 @@
1
+ require "digest/md5"
2
+
3
+ module S3diff
4
+ class LocalFile
5
+ def initialize(file_path)
6
+ @file_path = file_path
7
+ end
8
+
9
+ def etag
10
+ Digest::MD5.file(@file_path).to_s
11
+ end
12
+
13
+ def size
14
+ File.size(@file_path)
15
+ end
16
+
17
+ def path
18
+ @file_path
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ require "aws-sdk"
2
+ require "uri"
3
+
4
+ module S3diff
5
+ class S3Client
6
+ def initialize(options = {})
7
+ @client = Aws::S3::Client.new(options)
8
+ end
9
+
10
+ def head_object(s3_uri, options = {})
11
+ @client.head_object(options.merge(parse_s3_uri(s3_uri)))
12
+ end
13
+
14
+ def get_object(s3_uri, file_io, options = {})
15
+ @client.get_object(options.merge(parse_s3_uri(s3_uri))) do |chunk|
16
+ file_io.write(chunk)
17
+ end
18
+ end
19
+
20
+ def parse_s3_uri(s3_uri)
21
+ uri = URI.parse(s3_uri)
22
+ { bucket: uri.host, key: uri.path.sub(%r{^/}, "") }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ require "tempfile"
2
+
3
+ module S3diff
4
+ class S3File
5
+ def initialize(s3_uri, s3_client)
6
+ @s3c = s3_client
7
+ @uri = s3_uri
8
+ end
9
+
10
+ def etag
11
+ head.etag.delete(%("))
12
+ end
13
+
14
+ def size
15
+ head.content_length
16
+ end
17
+
18
+ def path
19
+ cache_file.path
20
+ end
21
+
22
+ private
23
+
24
+ def head
25
+ @head ||= @s3c.head_object(@uri)
26
+ end
27
+
28
+ def cache_file
29
+ @cache_file ||= begin
30
+ f = Tempfile.new("")
31
+ @s3c.get_object(@uri, f)
32
+ f.flush
33
+ f
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module S3diff
2
+ VERSION = "0.1.0"
3
+ end
data/lib/s3diff.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "s3diff/version"
2
+ require "s3diff/s3_client"
3
+ require "s3diff/s3_file"
4
+ require "s3diff/local_file"
5
+ require "s3diff/comparator"
data/s3diff.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "s3diff/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s3diff"
8
+ spec.version = S3diff::VERSION
9
+ spec.authors = ["masa21kik"]
10
+ spec.email = ["masa21kik@gmail.com"]
11
+
12
+ spec.summary = %q{Show difference between S3 file and local file.}
13
+ spec.description = %q{Show difference between S3 file and local file.}
14
+ spec.homepage = "https://github.com/masa21kik/s3diff"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "aws-sdk", "~> 2.10"
25
+ spec.add_dependency "diffy", "~> 3.2"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.15"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "test-unit", "~> 3.2"
30
+ spec.add_development_dependency "pry", "~> 0.10"
31
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3diff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - masa21kik
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: diffy
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: test-unit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.10'
97
+ description: Show difference between S3 file and local file.
98
+ email:
99
+ - masa21kik@gmail.com
100
+ executables:
101
+ - s3diff
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - exe/s3diff
114
+ - lib/s3diff.rb
115
+ - lib/s3diff/cli.rb
116
+ - lib/s3diff/comparator.rb
117
+ - lib/s3diff/local_file.rb
118
+ - lib/s3diff/s3_client.rb
119
+ - lib/s3diff/s3_file.rb
120
+ - lib/s3diff/version.rb
121
+ - s3diff.gemspec
122
+ homepage: https://github.com/masa21kik/s3diff
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.6.13
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Show difference between S3 file and local file.
146
+ test_files: []