Module: Eco::API::UseCases::GraphQL::Utils::Sftp

Includes:
Helpers::Base::CaseEnv
Defined in:
lib/eco/api/usecases/graphql/utils/sftp.rb

Instance Attribute Summary

Attributes included from Helpers::Base::ErrorHandling

#exception, #exiting

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Instance Method Details

#ensure_remote_empty(subfolder: remote_subfolder, pattern: nil) ⇒ Object



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
74
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 48

def ensure_remote_empty(subfolder: remote_subfolder, pattern: nil)
  files = with_remote_files(subfolder: subfolder, pattern: pattern)
  return if files.empty?

  msg  = "There are still files in the remote folder that will be deleted: '#{subfolder}':\n"
  msg << ""
  str  = files.map(&:longname).join("\n  • ")
  str << "\n"
  msg << str

  session.prompt_user(
    "Do you want to proceed to delete? (Y/n):",
    explanation: msg,
    default:     "Y",
    timeout:     3
  ) do |response|
    next unless response.upcase.start_with?("Y")

    files.each do |file|
      remote_full_path = to_remote_path(file.name, subfolder: subfolder)
      res              = sftp_session.remove(remote_full_path)
      log(:info) {
        "Deleted remote file: '#{remote_full_path}' (#{res})"
      }
    end
  end
end

#local_folderObject



112
113
114
115
116
117
118
119
120
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 112

def local_folder
  if (local_dir = options.dig(:sftp, :local_folder))
    local_dir
  elsif self.class.const_defined?(:LOCAL_FOLDER)
    self.class::LOCAL_FOLDER
  else
    "."
  end
end

#remote_folder(subfolder = remote_subfolder) ⇒ Object



104
105
106
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 104

def remote_folder(subfolder = remote_subfolder)
  [sftp_env_base_path, subfolder].compact.join('/')
end

#remote_subfolderObject



96
97
98
99
100
101
102
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 96

def remote_subfolder
  if (remote_dir = options.dig(:sftp, :remote_subfolder))
    remote_dir
  elsif self.class.const_defined?(:REMOTE_FOLDER)
    self.class::REMOTE_FOLDER
  end
end

#sftpObject



130
131
132
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 130

def sftp
  session.sftp
end

#sftp_configObject



122
123
124
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 122

def sftp_config
  session.config.sftp
end

#sftp_download_files(subfolder: remote_subfolder, pattern: nil, local_folder: self.local_folder, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 22

def sftp_download_files(subfolder: remote_subfolder, pattern: nil, local_folder: self.local_folder, &block)
  remote_files = with_remote_files(subfolder: subfolder, pattern: pattern)
  return [] if remote_files.empty?

  file_names = remote_files.map {|file| to_remote_path(file.name, subfolder: subfolder)}

  log(:info) {
    msg  = "Getting the following files into the local folder '#{local_folder}':\n"
    msg << ""
    msg << file_names.join("\n  • ")
    msg
  }

  sftp.download(file_names, local_folder: local_folder, &block)
end

#sftp_env_base_pathObject



108
109
110
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 108

def sftp_env_base_path
  sftp_config.remote_folder
end

#sftp_group_idObject



14
15
16
17
18
19
20
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 14

def sftp_group_id
  if self.class.const_defined?(:SFTP_GROUP)
    self.class.const_get(:SFTP_GROUP)
  elsif (group_id = options.dig(:sftp, :group))
    group_id
  end
end

#sftp_move_file(source, dest) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 38

def sftp_move_file(source, dest)
  sftp.move(source, dest, 0x0001) do |response|
    if response.ok?
      log(:info) { "#{source}\n  -to-> #{dest}"    }
    else
      log(:info) { "Could not move file #{source}" }
    end
  end
end

#sftp_sessionObject



126
127
128
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 126

def sftp_session
  sftp.sftp_session
end

#to_remote_path(file, subfolder: remote_subfolder) ⇒ Object



92
93
94
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 92

def to_remote_path(file, subfolder: remote_subfolder)
  [remote_folder(subfolder), file].join('/')
end

#upload(local_file, subfolder: remote_subfolder, gid: sftp_group_id) ⇒ Object Also known as: sftp_upload



5
6
7
8
9
10
11
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 5

def upload(local_file, subfolder: remote_subfolder, gid: sftp_group_id)
  sftp_session.upload(
    local_file,
    remote_folder: remote_folder(subfolder),
    gid:           gid
  )
end

#with_remote_files(subfolder: remote_subfolder, pattern: nil) ⇒ Object

Parameters:

  • subfolder (String) (defaults to: remote_subfolder)

    the subfolder.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 77

def with_remote_files(subfolder: remote_subfolder, pattern: nil)
  sftp.files(remote_folder(subfolder), pattern: pattern).each do |remote_file|
    yield(remote_file) if block_given?
  end
rescue ArgumentError
  raise
rescue ::Net::SFTP::StatusException => err
  log(:error) {
    msg  = "(#{self.class}) There was an error trying to access "
    msg << "the remote folder '#{subfolder}': #{err}"
    msg
  }
  []
end