Skip to content

Commit

Permalink
Add the 'add-line' and 'remove-line' tools.
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarWidget committed Feb 2, 2012
1 parent cda2e05 commit 6f31760
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions add-line
@@ -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
22 changes: 22 additions & 0 deletions remove-line
@@ -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.