require 'net/sftp'
class Net::SFTP::Session
def rm_r!(path)
self.dir.entries(path).each do |entry|
next if entry.name == '.' or entry.name == '..'
child_path = File.join(path, entry.name)
if self.stat!(child_path).directory?
self.rm_r!(child_path)
else
puts "remove! #{child_path}"
self.remove!(child_path)
end
end
puts "rmdir! #{path}"
self.rmdir!(path)
end
def clear!(path)
puts "clear! #{path}"
self.dir.entries(path).each do |entry|
self.rm_r!(File.join(path, entry.name))
end
end
end
Refactorings
No refactoring yet !
Seanbo
September 3, 2009, September 03, 2009 23:03, permalink
Why not just exec an rsync from the shell and send the command over an ssh tunnel? I realize the point is to refactor the code, but why write a pure ruby script that could potentially be broken by changes to net/sftp? This assumes you've done a key exchange between the local and remote machines...
`rsync --delete -a /path/to/some/emptydir/ <remoteserver>:<user>/remote/dir/to/clear/`
I'm trying to clear a remote directory via sftp. I'm new to ruby and net-sftp so it wouldn't surprise me if there is a more elegant or built in way to do this. Also, it feels like there might be a faster way. I've extended the net::sftp::session in the following way. Thoughts?