def run
require 'fog'
src = File.expand_path(self.source_path)
bucket = self.config.delete(:bucket) || self.config.delete(:bucket_name)
path = self.config[:path]
self.config.delete(:kind)
error 'The path requires no trailing slash' if path && path[-1,1] == '/'
if self.dry_run?
::Fog.mock!
end
puts "Connecting"
connection = ::Fog::Storage.new(self.config)
puts "Getting bucket"
begin
directory = connection.directories.get(bucket)
rescue ::Excon::Errors::NotFound
should_create_bucket = true
end
should_create_bucket = true if directory.nil?
if should_create_bucket
directory = connection.directories.create(:key => bucket)
end
files = directory.files
truncated = files.respond_to?(:is_truncated) && files.is_truncated
while truncated
set = directory.files.all(:marker => files.last.key)
truncated = set.is_truncated
files = files + set
end
keys_to_destroy = files.all.map {|file| file.key}
puts "Uploading local files"
FileUtils.cd(src) do
files = Dir['**/*'].select { |f| File.file?(f) }
files.each do |file_path|
key = "#{path}#{file_path}"
directory.files.create(
:key => key,
:body => File.open(file_path),
:public => true)
keys_to_destroy.delete(key)
end
end
puts "Removing remote files"
keys_to_destroy.each do |key|
directory.files.get(key).destroy
end
puts "Done!"
end