Class: S3aps::Sync
- Inherits:
-
Object
- Object
- S3aps::Sync
- Defined in:
- lib/s3aps/sync.rb
Overview
The controller for everything else.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Sync
constructor
A new instance of Sync.
-
#list ⇒ Object
Merge the local and remote lists into one and indicate whether each item is local, remote or both.
-
#pull ⇒ Object
Get all the remote files and write them out locally, if they don’t already exist.
-
#push ⇒ Object
Get all the local files, recursively, and write them to S3, if they don’t already exist.
Constructor Details
#initialize(options = {}) ⇒ Sync
Returns a new instance of Sync.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/s3aps/sync.rb', line 22 def initialize( = {}) = S3aps::symbolize_keys() s3_file = [[:config], "s3.yml", "config/s3.yml"].detect {|f| f && File.file?(f) } if s3_file puts "Configuring S3 from #{s3_file}" = S3aps::symbolize_keys YAML.load_file(s3_file) env = [:env] if env && [env.to_sym] = S3aps::symbolize_keys [env.to_sym] puts "Using #{env} environment" end # Support for alias: access_key_id = key if [:access_key_id] [:key] ||= [:access_key_id] .delete(:access_key_id) end # Support for alias: secret_access_key = secret if [:secret_access_key] [:secret] ||= [:secret_access_key] .delete(:secret_access_key) end .merge!() {|k,o,n| o } end @s3_adapter = S3aps::S3Adapter.new @file_adapter = S3aps::FileAdapter.new end |
Instance Method Details
#list ⇒ Object
Merge the local and remote lists into one and indicate whether each item is local, remote or both
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/s3aps/sync.rb', line 51 def list local = @file_adapter.list remote = @s3_adapter.list.map{|i| i[:key]} all = (local + remote).uniq.sort all.map do |path| [ local.include?(path) ? "L" : " ", remote.include?(path) ? "R" : " ", path ] end end |
#pull ⇒ Object
Get all the remote files and write them out locally, if they don’t already exist. For simplicity, we assume that if a file of the same name already exists then it is the same as the remote one.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/s3aps/sync.rb', line 67 def pull check_list = @file_adapter.list count = 0 total = 0 @s3_adapter.list.each do |o| if !check_list.include?(o[:key]) $stdout.print "*" if @file_adapter.write(o[:key], @s3_adapter.read(o[:key]), o[:etag]) count += 1 end else $stdout.print "." end $stdout.flush total += 1 end [count, total] end |
#push ⇒ Object
Get all the local files, recursively, and write them to S3, if they don’t already exist. For simplicity, we assume that if a file of the same name already exists then it is the same as the local one.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/s3aps/sync.rb', line 89 def push check_list = @s3_adapter.list.map{|i| i[:key]} count = 0 total = 0 @file_adapter.list.each do |path| if !check_list.include?(path) $stdout.print "*" if @s3_adapter.write(path, @file_adapter.read(path)) count += 1 end else $stdout.print "." end $stdout.flush total += 1 end [count, total] end |