class Dandelion::Adapter::SFTP

Public Class Methods

new(config) click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 12
def initialize(config)
  begin
    require 'highline/import'
  rescue LoadError
  end
  
  require 'net/sftp'

  @config = config
  @config.defaults(preserve_permissions: true)

  @sftp = sftp_client
end

Public Instance Methods

delete(file) click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 54
def delete(file)
  begin
    @sftp.remove!(path(file))
    cleanup(File.dirname(path(file)))
  rescue Net::SFTP::StatusException => e
    raise unless e.code == 2
  end
end
read(file) click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 26
def read(file)
  begin
    @sftp.file.open(path(file), 'r') do |f|
      f.gets
    end
  rescue Net::SFTP::StatusException => e
    raise unless e.code == 2
    nil
  end
end
to_s() click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 76
def to_s
  "sftp://#{@config['username']}@#{@config['host']}/#{@config['path']}"
end
write(file, data) click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 37
def write(file, data)
  temp(file, data) do |temp|
    begin
      @sftp.upload!(temp, path(file))
    rescue Net::SFTP::StatusException => e
      raise unless e.code == 2
      mkdir_p(File.dirname(path(file)))
      @sftp.upload!(temp, path(file))
    end
  end

  if @config[:preserve_permissions]
    mode = get_mode(file)
    @sftp.setstat!(path(file), permissions: mode) if mode
  end
end

Private Instance Methods

cleanpath(path) click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 96
def cleanpath(path)
  Pathname.new(path).cleanpath.to_s if path
end
cleanup(dir) click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 100
def cleanup(dir)
  unless cleanpath(dir) == cleanpath(@config['path']) or dir == File.dirname(dir)
    if empty?(dir)
      @sftp.rmdir!(dir)
      cleanup(File.dirname(dir))
    end
  end
end
empty?(dir) click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 109
def empty?(dir)
  @sftp.dir.entries(dir).delete_if do |file|
    file.name == '.' or file.name == '..'
  end.empty?
end
get_mode(file) click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 91
def get_mode(file)
  stat = File.stat(file) if File.exists?(file)
  stat.mode if stat
end
mkdir_p(dir) click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 115
def mkdir_p(dir)
  begin
    @sftp.mkdir!(dir)
  rescue Net::SFTP::StatusException => e
    raise unless e.code == 2
    mkdir_p(File.dirname(dir))
    @sftp.mkdir!(dir)
  end
end
path(file) click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 125
def path(file)
  if @config['path'] and !@config['path'].empty?
    File.join(@config['path'], file)
  else
    file
  end
end
sftp_client() click to toggle source
# File lib/dandelion/adapter/sftp.rb, line 82
def sftp_client
  options = {
    password: @config['password'],
    port: @config['port'] || Net::SSH::Transport::Session::DEFAULT_PORT,
  }

  Net::SFTP.start(@config['host'], @config['username'], options)
end