Permalink
Browse files

Add the 'add-line' and 'remove-line' tools.

  • Loading branch information...
1 parent cda2e05 commit 6f31760e3acb6fedb57bf9c8ddf4497dc7241415 @FooBarWidget FooBarWidget committed Feb 2, 2012
Showing with 64 additions and 1 deletion.
  1. +17 −1 README.md
  2. +25 −0 add-line
  3. +22 −0 remove-line
View
@@ -104,9 +104,25 @@ You must also make sure your filesystem is mounted with ACL support, e.g.:
Don't forget to update /etc/fstab too.
+### add-line
+
+Adds a line to the given file if it doesn't already include it.
+
+ /tools/add-line foo.log "hello world"
+ # Same effect:
+ /tools/add-line foo.log hello world
+
+### remove-line
+
+Removes the first instance of a line from the given file. Does nothing if the file doesn't include that line.
+
+ /tools/remove-line foo.log "hello world"
+ # Same effect:
+ /tools/remove-line foo.log hello world
+
### truncate
-Truncates all passed files to 0 bytes.
+Truncates all given files to 0 bytes.
## RabbitMQ
View
@@ -0,0 +1,25 @@
+#!/usr/bin/env ruby
+# encoding: utf-8
+
+def add_line(filename, line)
+ lines = File.open(filename, 'r') do |f|
+ f.binmode
+ f.read.split("\n", -1)
+ end
+ if !lines.include?(line)
+ if lines.last && lines.last.empty?
+ lines.pop
+ end
+ lines << "#{line}\n"
+ File.open(filename, "w") do |f|
+ f.binmode
+ f.write(lines.join("\n"))
+ end
+ end
+end
+
+if ARGV.size < 2
+ abort "Usage: add-line <FILENAME> <LINE>"
+else
+ add_line(ARGV[0], ARGV[1 .. ARGV.size - 1].join(" "))
+end
View
@@ -0,0 +1,22 @@
+#!/usr/bin/env ruby
+# encoding: utf-8
+
+def remove_line(filename, line)
+ lines = File.open(filename, "r") do |f|
+ f.binmode
+ f.read.split("\n", -1)
+ end
+ if index = lines.find_index(line)
+ lines.delete_at(index)
+ end
+ File.open(filename, "w") do |f|
+ f.binmode
+ f.write(lines.join("\n"))
+ end
+end
+
+if ARGV.size < 2
+ abort "Usage: remove-line <FILENAME> <LINE>"
+else
+ remove_line(ARGV[0], ARGV[1 .. ARGV.size - 1].join(" "))
+end

0 comments on commit 6f31760

Please sign in to comment.