class Fastlane::Sftp::Uploader

Responsible for performing upload SFTP operations

Attributes

delete_target_dir[RW]
files[RW]
host[RW]

These want to be an input parameters:

options[RW]
password[RW]
port[RW]
root_path[RW]
rsa_keypath[RW]
rsa_keypath_passphrase[RW]
target_dir[RW]
user[RW]

Public Class Methods

new(options) click to toggle source
# File lib/fastlane/plugin/sftp/helper/uploader.rb, line 30
def initialize(options)
  self.options = options unless options.nil?
  self.host = options[:server_url]
  self.port = options[:server_port]
  self.user = options[:server_user]
  self.password = options[:server_password]
  self.rsa_keypath = options[:server_key]
  self.rsa_keypath_passphrase = options[:server_key_passphrase]
  self.files = options[:file_paths]
  self.target_dir = options[:target_dir]
  self.delete_target_dir = options[:delete_target_dir]
end

Public Instance Methods

upload() click to toggle source

Upload files

# File lib/fastlane/plugin/sftp/helper/uploader.rb, line 47
def upload
  # Login & Upload all files using RSA key or username/password
  UI.message('upload...')

  session = Helper::SftpHelper.login(host, port, user, password, rsa_keypath, rsa_keypath_passphrase)
  UI.message('Uploading files...')

  session.sftp.connect do |sftp|
    Helper::SftpHelper.remote_rmdir(sftp, target_dir) if delete_target_dir
    Helper::SftpHelper.remote_mkdir(sftp, target_dir)
    uploads = []
    files.each do |file|
      next unless Helper::SftpHelper.check_file(file)

      re = Helper::SftpHelper.get_target_file_path(file, target_dir)
      if File.directory?(file)
        Helper::SftpHelper.remote_mkdir(sftp, re)
      end
      uploads.push(upload_file(sftp, file, re))
    end
    uploads.each(&:wait)

    # Lists the entries in a directory for verification
    sftp.dir.foreach(target_dir) do |entry|
      UI.message(entry.longname)
    end
  end
  session.close
  return true
end

Private Instance Methods

upload_file(sftp, local_file_path, remote_file_path) click to toggle source

Upload file

@param sftp @param [String] local_file_path @param [String] remote_file_path

# File lib/fastlane/plugin/sftp/helper/uploader.rb, line 85
def upload_file(sftp, local_file_path, remote_file_path)
  if File.file?(local_file_path)
    type = "file"
  else
    type = "folder"
  end
  UI.message("starting upload of #{type} #{local_file_path} to #{remote_file_path}")
  return sftp.upload(local_file_path, remote_file_path) do |event, _uploader, *args|
    case event
    when :mkdir then
      # args[0] : remote path name
      UI.message("creating directory #{args[0]}")
    when :open then
      file = args[0]
      UI.message("starting upload of #{file.local} to #{file.remote}")
    when :close then
      file = args[0]
      UI.message("finished with #{file.remote}")
    when :finish then
      UI.success("upload of #{type} #{local_file_path} to #{remote_file_path} successful")
    end
  end
end