/
rsync.rb
37 lines (31 loc) · 1.05 KB
/
rsync.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module SupplyDrop
class Rsync
class << self
def command(from, to, options={})
flags = ['-az']
flags << '--delete' if options[:delete]
flags << excludes(options[:excludes]) if options.has_key?(:excludes)
flags << ssh_options(options[:ssh]) if options.has_key?(:ssh)
"rsync #{flags.compact.join(' ')} #{from} #{to}"
end
def remote_address(user, host, path)
user_with_host = [user, host].compact.join('@')
[user_with_host, path].join(':')
end
def excludes(patterns)
[patterns].flatten.map { |p| "--exclude=#{p}" }
end
def ssh_options(options)
mapped_options = options.map do |key, value|
next unless value
case key
when :keys then [value].flatten.select { |k| File.exist?(k) }.map { |k| "-i #{k}" }
when :config then "-F #{value}"
when :port then "-p #{value}"
end
end.compact
%[-e "ssh #{mapped_options.join(' ')}"] unless mapped_options.empty?
end
end
end
end