0
+ class SFTP
< Generic #:nodoc:
0
+ COMPONENT = [ :scheme, :userinfo, :host, :port, :path ].freeze
0
# Caching of passwords, so we only need to ask once.
0
- def
write_internal(options, &block) #:nodoc:0
+ def
read(options = {}, &block)0
# SSH options are based on the username/password from the URI.
0
- ssh_options = { :port=>port, :
username=>user, :password=>password }.merge(options[:ssh_options] || {})
0
+ ssh_options = { :port=>port, :
, :password=>password }.merge(options[:ssh_options] || {})
0
ssh_options[:password] ||= SFTP.passwords[host]
0
puts "Connecting to #{host}" if Buildr.application.options.trace
0
- session = Net::SSH.start(host, ssh_options)
0
- SFTP.passwords[host] = ssh_options[:password]
0
+ Net::SFTP.start(host, user, ssh_options) do |sftp|
0
+ SFTP.passwords[host] = ssh_options[:password]
0
+ puts 'connected' if Buildr.application.options.trace
0
+ with_progress_bar options[:progress] && options[:size], path.split('/'), options[:size] || 0 do |progress|
0
+ puts "Downloading to #{path}" if Buildr.application.options.trace
0
+ sftp.file.open(path, 'r') do |file|
0
+ while chunk = file.read(32 * 4096)
0
+ while chunk = file.read(32 * 4096)
0
rescue Net::SSH::AuthenticationFailed=>ex
0
# Only if running with console, prompt for password.
0
if !ssh_options[:password] && $stdout.isatty
0
- session.sftp.connect do |sftp|
0
- puts 'connected' if Buildr.application.options.trace
0
- # To create a path, we need to create all its parent. We use realpath to determine if
0
- # the path already exists, otherwise mkdir fails.
0
- puts "Creating path #{path}" if Buildr.application.options.trace
0
- File.dirname(path).split('/').inject('') do |base, part|
0
- combined = base + part
0
- sftp.realpath combined rescue sftp.mkdir combined, {}
0
+ def write_internal(options, &block) #:nodoc:
0
+ # SSH options are based on the username/password from the URI.
0
+ ssh_options = { :port=>port, :password=>password }.merge(options[:ssh_options] || {})
0
+ ssh_options[:password] ||= SFTP.passwords[host]
0
+ puts "Connecting to #{host}" if Buildr.application.options.trace
0
+ Net::SFTP.start(host, user, ssh_options) do |sftp|
0
+ SFTP.passwords[host] = ssh_options[:password]
0
+ puts 'connected' if Buildr.application.options.trace
0
- with_progress_bar options[:progress] && options[:size], path.split('/'), options[:size] || 0 do |progress|
0
- puts "Uploading to #{path}" if Buildr.application.options.trace
0
- sftp.open_handle(path, 'w') do |handle|
0
- # Writing in chunks gives us the benefit of a progress bar,
0
- # but also require that we maintain a position in the file,
0
- # since write() with two arguments always writes at position 0.
0
- while chunk = yield(32 * 4096)
0
- sftp.write(handle, chunk, pos)
0
+ # To create a path, we need to create all its parent. We use realpath to determine if
0
+ # the path already exists, otherwise mkdir fails.
0
+ puts "Creating path #{path}" if Buildr.application.options.trace
0
+ File.dirname(path).split('/').inject('') do |base, part|
0
+ combined = base + part
0
+ sftp.realpath combined rescue sftp.mkdir combined, {}
0
+ with_progress_bar options[:progress] && options[:size], path.split('/'), options[:size] || 0 do |progress|
0
+ puts "Uploading to #{path}" if Buildr.application.options.trace
0
+ sftp.file.open(path, 'w') do |file|
0
+ while chunk = yield(32 * 4096)
0
+ sftp.setstat(path, :permissions => options[:permissions]) if options[:permissions]
0
- sftp.setstat(path, :permissions => options[:permissions]) if options[:permissions]
0
+ rescue Net::SSH::AuthenticationFailed=>ex
0
+ # Only if running with console, prompt for password.
0
+ if !ssh_options[:password] && $stdout.isatty
0
+ password = ask("Password for #{host}:") { |q| q.echo = '*' }
0
+ ssh_options[:password] = password
0
+ @@schemes['SFTP'] = SFTP
0
# File URL. Keep in mind that file URLs take the form of <code>file://host/path</code>, although the host
Comments
No one has commented yet.