class RHC::Commands::Scp
Public Instance Methods
run(_, action, local_path, remote_path)
click to toggle source
# File lib/rhc/commands/scp.rb, line 28 def run(_, action, local_path, remote_path) rest_app = find_app ssh_opts = rest_app.ssh_url.gsub("ssh://","").split("@") raise RHC::ArgumentNotValid.new("'#{action}' is not a valid argument for this command. Please use upload or download.") unless action == 'download' || action == 'upload' raise RHC::FileOrPathNotFound.new("Local file, file_path, or directory could not be found.") unless File.exist?(local_path) begin start_time = Time.now last_sent = nil Net::SCP.send("#{action}!".to_sym, ssh_opts[1], ssh_opts[0], (action == 'upload' ? local_path : remote_path), (action == 'upload' ? remote_path : local_path)) do |ch, name, sent, total| #:nocov: if sent != last_sent last_sent = sent complete = total == 0 ? 100 : ((sent.to_f/total.to_f)*100).to_i $stderr.print "\r #{action}ing #{name}: #{complete}% complete. #{sent}/#{total} bytes transferred " + (sent == total ? "in #{Time.now - start_time} seconds \n" : "") end #:nocov: end rescue Errno::ECONNREFUSED raise RHC::SSHConnectionRefused.new(ssh_opts[0], ssh_opts[1]) rescue SocketError => e raise RHC::ConnectionFailed, "The connection to #{ssh_opts[1]} failed: #{e.message}" rescue Net::SSH::AuthenticationFailed => e debug_error e raise RHC::SSHAuthenticationFailed.new(ssh_opts[1], ssh_opts[0]) rescue Net::SCP::Error => e debug_error e raise RHC::RemoteFileOrPathNotFound.new("An unknown error occurred: #{e.message}") end end