Class: Dandelion::Adapter::SFTP

Inherits:
Base
  • Object
show all
Includes:
Utils
Defined in:
lib/dandelion/adapter/sftp.rb

Instance Method Summary collapse

Methods included from Utils

#temp

Methods inherited from Base

adapter, create_adapter, requires_gems

Constructor Details

#initialize(config) ⇒ SFTP

Returns a new instance of SFTP.



12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

#delete(file) ⇒ Object



54
55
56
57
58
59
60
61
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# 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


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dandelion/adapter/sftp.rb', line 63

def symlink(file, target)
  begin
    @sftp.symlink!(target, path(file))
  rescue Net::SFTP::StatusException => e
    if e.code == 2
      mkdir_p(File.dirname(path(file)))
    else
      @sftp.remove!(path(file))
    end
    @sftp.symlink!(target, path(file))
  end
end

#to_sObject



76
77
78
# File 'lib/dandelion/adapter/sftp.rb', line 76

def to_s
  "sftp://#{@config['username']}@#{@config['host']}/#{@config['path']}"
end

#write(file, data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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