Permalink
Browse files

Added file_append method

  • Loading branch information...
1 parent d7146cc commit 82cede0b12f1927cb8b6019950909938ab794a6a @delano committed May 30, 2009
Showing with 75 additions and 4 deletions.
  1. +3 −3 CHANGES.txt
  2. +1 −1 lib/rye/box.rb
  3. +18 −0 lib/rye/cmd.rb
  4. +53 −0 tst/65_rbox_file_append_test.rb
View
@@ -11,13 +11,13 @@ TODO
* CHANGE: Rye::Box now uses unique instance variable names to encourage using
instance variables in batch command blocks.
-
+* ADDED: Rye::Box#file_append
+
#### 0.6.6 (2009-05-21) #############################
* CHANGE: Key management is handled by ssh-agent again (instead of Net::SSH)
-
#### 0.6.5 (2009-05-10) #############################
* CHANGE: Default exit code is now 0 instead of -1
@@ -44,14 +44,14 @@ TODO
* FIXED: I forgot to add highline to the gemspec file manifest. Cripes!
-
#### 0.6.1 (2009-04-29) #############################
* ADDED: Prints message to STDERR when passwordless login fails.
* ADDED: Highline 1.5.1 to vendor to fix the Ruby 1.9 issue (Highline
1.5.1 is not released as a gem yet)
* CHANGE: Cleaned examples and links in README
+
#### 0.6.0 (2009-04-28) #############################
* FIXED: handling of Process::Status ($?) in Rye.shell
View
@@ -728,7 +728,7 @@ def net_scp_transfer!(direction, *files)
raise "Cannot upload to a StringIO object"
end
- # Fail early. We check the
+ # Fail early. We check whether the StringIO object is available to read
files.each do |file|
if file.is_a?(StringIO)
raise "Cannot download a StringIO object" if direction == :download
View
@@ -98,6 +98,24 @@ def upload(*files); net_scp_transfer!(:upload, *files); end
# NOTE: Changes to current working directory with +cd+ or +[]+ are ignored.
def download(*files); net_scp_transfer!(:download, *files); end
+
+ def file_append(filepath, newcontent, backup=false)
+ if self.file_exists?(filepath) && backup
+ self.cp filepath, "#{filepath}-previous"
+ end
+
+ file_content = self.download filepath
+ file_content ||= StringIO.new
+ if newcontent.is_a?(StringIO)
+ newcontent.rewind
+ file_content.puts newcontent.read
+ else
+ file_content.puts newcontent
+ end
+
+ self.upload file_content, filepath
+ end
+
# Does a remote path exist?
def file_exists?(path)
begin
@@ -0,0 +1,53 @@
+#!/usr/bin/ruby
+
+#
+# Usage: test/65_rbox_file_append_test.rb
+#
+
+$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
+
+require "rubygems"
+require "stringio"
+require "yaml"
+require "rye"
+
+tmpdir = Rye.sysinfo.tmpdir
+
+rbox = Rye::Box.new("localhost", :info => false)
+def rbox.rm(*args); cmd('rm', args); end
+rbox.rm(:r, :f, "#{tmpdir}/rye-upload") # Silently delete test dir
+rbox.mkdir("#{tmpdir}/rye-upload")
+
+# Create a file with one line so we have something to append to.
+initfile = StringIO.new
+initfile.puts "Initial file content (before append)"
+rbox.upload(initfile, "#{tmpdir}/rye-upload/initfile")
+
+# Append a single line to the file
+rbox.file_append("#{tmpdir}/rye-upload/initfile", "APPENDED: a single line")
+
+puts $/, "SHOULD BE 2 lines"
+puts rbox.cat("#{tmpdir}/rye-upload/initfile")
+
+puts $/, "THE LAST APPEND DID NOT REQUEST A BACKUP. SUCCESS? (should be false):"
+puts rbox.file_exists?("#{tmpdir}/rye-upload/initfile-previous")
+
+
+# Append multiple lines from an Array
+rbox.file_append("#{tmpdir}/rye-upload/initfile", ['line3', 'line4'])
+
+puts $/, "SHOULD BE 4 lines"
+puts rbox.cat("#{tmpdir}/rye-upload/initfile")
+
+junk = StringIO.new
+junk.puts('line5')
+junk.puts('line6')
+junk.puts('line7')
+rbox.file_append("#{tmpdir}/rye-upload/initfile", junk, :backup)
+
+puts $/, "SHOULD BE 7 lines"
+puts rbox.cat("#{tmpdir}/rye-upload/initfile")
+
+puts $/, "THE LAST APPEND REQUESTED A BACKUP. SUCCESS? (should be true):"
+puts rbox.file_exists?("#{tmpdir}/rye-upload/initfile-previous")
+

0 comments on commit 82cede0

Please sign in to comment.