class Squarespace::Sync::Connection

Public Class Methods

new(&block) click to toggle source
# File lib/squarespace/sync/connection.rb, line 7
def initialize(&block)
  options = Squarespace::Sync.options

  Net::SFTP.start(options[:host], options[:username], password: options[:password], port: options[:port]) do |sftp|
    block.call( sftp, options, self )
  end
end

Public Instance Methods

pull_file( sftp, remote, local ) click to toggle source
# File lib/squarespace/sync/connection.rb, line 30
def pull_file( sftp, remote, local )
  if File.exists?( local )
    overwrite = ask("#{local} already exists. Overwrite it?", :yellow, limited_to: %w{y n})
    if overwrite == "y"
      say("Downloading #{remote}", :green)
      sftp.download!(remote, local)
    end
  else
    say("Downloading #{remote}", :green)
    sftp.download!(remote, local)
  end
end
pull_tree( sftp, directory ) click to toggle source
# File lib/squarespace/sync/connection.rb, line 17
def pull_tree( sftp, directory )
  sftp.dir[directory, "**/*"].each do |entry|
    remote = File.join(directory, entry.name)
    local = File.join(Squarespace::Sync.local_path, entry.name)

    if sftp.file.directory?( remote )
      FileUtils.mkdir_p( local )
    else
      pull_file( sftp, remote, local )
    end
  end
end
push_file( sftp, local, remote ) click to toggle source
# File lib/squarespace/sync/connection.rb, line 57
def push_file( sftp, local, remote )
  say("Uploading #{remote}", :green)

  begin
    sftp.upload!( local, remote )
  rescue Net::SFTP::StatusException => code
    sftp.remove!( remote ) do
      sftp.upload!( local, remote )
    end if code == 11
  end
end
push_tree( sftp, directory ) click to toggle source
# File lib/squarespace/sync/connection.rb, line 43
def push_tree( sftp, directory )
  Dir[File.join(Squarespace::Sync.local_path, "**/*")].each do |local|
    remote = local.gsub(Squarespace::Sync.local_path + "/", "")
    absolute_remote = File.join(directory, remote)
    next if Squarespace::Sync.options[:ignore].include?( remote.split("/").first )

    if File.directory?( local )
      sftp.mkdir!( absolute_remote ) unless sftp.file.directory?( absolute_remote )
    else
      push_file( sftp, local, absolute_remote)
    end
  end
end