Sha256: 1ba9fd58f210db4932323aea88e7bbe8cd3186fd8ee65717e5177b8448ba3d0b

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'net/sftp'
require 'ptools'

module Middleman
  module Deploy
    module Methods
      class Sftp < Ftp
        def process
          puts "## Deploying via sftp to #{user}@#{host}:#{path}"

          # `nil` is a valid value for user and/or pass.
          Net::SFTP.start(host, user, password: pass, port: port) do |sftp|
            sftp.mkdir(path)

            Dir.chdir(build_dir) do
              filtered_files.each do |filename|
                if File.directory?(filename)
                  upload_directory(sftp, filename)
                else
                  upload_file(sftp, filename)
                end
              end
            end
          end
        end

        protected

        def handle_exception(exception, filename, file_path)
          reply     = exception.message
          err_code  = reply[0, 3].to_i

          sftp.upload(filename, file_path) if err_code == 550
        end

        def upload_directory(sftp, filename)
          file_path = "#{path}/#{filename}"

          begin
            sftp.mkdir(file_path)
            puts "Created directory #{filename}"
          rescue
          end
        end

        def upload_file(sftp, filename)
          file_path = "#{path}/#{filename}"

          begin
            sftp.upload(filename, file_path)
          rescue Exception => exception
            handle_exception(exception, filename, file_path)
          end

          puts "Copied #{filename}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
middleman-deploy-2.0.0.pre.alpha lib/middleman-deploy/methods/sftp.rb