class Sftp::Sync::Client

Attributes

config[R]
filter[R]
session[R]

Public Class Methods

new(_config) click to toggle source
# 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

Public Instance Methods

close() click to toggle source
# File lib/sftp/sync/client.rb, line 29
def close

  @session.close
end
diff() click to toggle source
# 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}" if config.verbose?
      when :remote
        puts "+  #{entry.name}"
      end

    end
  end

  diff_map.local_entries.each do |entry|
 
    puts "-  #{entry.name}"
  
  end
end
list() click to toggle source
# 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() click to toggle source
# File lib/sftp/sync/client.rb, line 77
def pull
  
  downloads = []

  diff_map = build_diff_map

  output_path = File.expand_path(config.output_dir)

  # list the entries in a directory
  session.dir.glob("/", "**/*") do |entry|
   
    unless entry.directory? || filter.match(entry.name)

      src = entry.name
      dest = File.join(output_path, 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(output_path, 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

Private Instance Methods

build_diff_map() click to toggle source
# File lib/sftp/sync/client.rb, line 138
def build_diff_map
  diff_map = FileDiffMap.new
   
  output_pathname = Pathname.new(File.expand_path(config.output_dir))

  ## enumerate local files
  Dir[ File.join(output_pathname.to_s, '**', '*') ].each do |local_entry|



    relative_path = Pathname.new(local_entry).relative_path_from(output_pathname).to_s

    diff_map.add(relative_path, (File.size?(local_entry)|| 0))  unless File.directory?(local_entry) || filter.match(relative_path)
  end

  diff_map
end