Class: Sftp::Sync::Client
- Inherits:
-
Object
- Object
- Sftp::Sync::Client
- Defined in:
- lib/sftp/sync/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#filter ⇒ Object
readonly
Returns the value of attribute filter.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
- #close ⇒ Object
- #diff ⇒ Object
-
#initialize(_config) ⇒ Client
constructor
A new instance of Client.
- #list ⇒ Object
- #pull ⇒ Object
Constructor Details
#initialize(_config) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sftp/sync/client.rb', line 17 def initialize(_config) @config = _config @session = Net::SFTP.start(config.host, config.username, config.sftp_opts) # set filter @filter = Filters::Pass.new if config.filter? @filter = Filters::GitIgnore.new(IO.readlines(config.filter)) end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/sftp/sync/client.rb', line 14 def config @config end |
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
14 15 16 |
# File 'lib/sftp/sync/client.rb', line 14 def filter @filter end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
14 15 16 |
# File 'lib/sftp/sync/client.rb', line 14 def session @session end |
Instance Method Details
#close ⇒ Object
29 30 31 32 |
# File 'lib/sftp/sync/client.rb', line 29 def close @session.close end |
#diff ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/sftp/sync/client.rb', line 47 def diff diff_map = build_diff_map # list the entries in a directory session.dir.glob("/", "**/*") do |entry| unless entry.directory? || filter.match(entry.name) case diff_map.update(entry.name , entry.attributes.size ) when :diff puts "* #{entry.name}" when :match puts " #{entry.name}" when :remote puts "+ #{entry.name}" end end end diff_map.local_entries.each do |entry| puts "- #{entry.name}" end end |
#list ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sftp/sync/client.rb', line 34 def list # list the entries in a directory session.dir.glob("/", "**/*") do |entry| puts entry.name unless entry.directory? || filter.match(entry.name) end end |
#pull ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/sftp/sync/client.rb', line 77 def pull downloads = [] diff_map = build_diff_map # list the entries in a directory session.dir.glob("/", "**/*") do |entry| unless entry.directory? || filter.match(entry.name) src = entry.name dest = File.join(config.output_dir, entry.name) dest_dir = File.dirname(dest) FileUtils.mkdir_p dest_dir unless Dir.exists?(dest_dir) puts "#{src} => #{dest}" downloads << session.download(src , dest) ## diff_map.update(entry.name , entry.attributes.size ) end end downloads.each { |d| d.wait } diff_map.local_entries.each do |entry| target = File.join(config.output_dir, entry.name) if config.remove_local? if File.exist?(target) puts "removing #{target}" File.delete(target) end else puts "#{target} in local but not remote, use --remove_local flag to auto remove" end end end |