change variables to point to option parser, updated readme

This commit is contained in:
Rilindo Foster 2016-05-20 22:10:56 -05:00
parent 1c93e8bb47
commit 76a332e7d0
2 changed files with 30 additions and 28 deletions

View File

@ -10,13 +10,19 @@ or more of these older versions.
Run the script like this: Run the script like this:
clientbucket.rb /path/to/file clientbucket.rb -t /path/to/file -c path/to/client/bucket
Where /path/to/file is the file on the system that you're looking for old versions of. Where /path/to/file is the file on the system that you're looking for old versions of. If you run it by itself, it will return the following help screen:
Usage: clientbucket.rb -t target_path -c clientbucket_path
-t, --target_path target_path path of the file to restore
-c clientbucket_path, where to restore from. Defaults to /var/lib/puppet/clientbucket
--clientbucket_path
-h, --help
The script runs interactively and prompts you for what you want to do. An example: The script runs interactively and prompts you for what you want to do. An example:
# clientbucket.rb /some/test/file # clientbucket.rb -t /some/test/file
[0]: 41f59421026a473a0378c58d539069c6 Thu Feb 09 15:56:36 +0000 2012 [0]: 41f59421026a473a0378c58d539069c6 Thu Feb 09 15:56:36 +0000 2012
[1]: 6bc6ab38660066ea8cf0743b889bd075 Mon Feb 20 15:23:59 +0000 2012 [1]: 6bc6ab38660066ea8cf0743b889bd075 Mon Feb 20 15:23:59 +0000 2012
[2]: 74da6d605bfd7ecad38904bc35a0292a Thu May 24 13:45:10 +0100 2012 [2]: 74da6d605bfd7ecad38904bc35a0292a Thu May 24 13:45:10 +0100 2012

View File

@ -1,42 +1,38 @@
#!/usr/bin/ruby #!/usr/bin/env ruby
require 'rubygems' require 'rubygems'
require 'find' require 'find'
require 'optparse' require 'optparse'
ARGV << '-h' if ARGV.empty?
options = {:target_path => nil, :clientbucket_path => nil} options = {:target_path => nil, :clientbucket_path => nil}
parser = OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "Usage: clientbucket.rb -t target_path -c clientbucket_path" opts.banner = "Usage: clientbucket.rb -t target_path -c clientbucket_path"
opts.on("-t", "--target_path target_path", "path to file to restore") do |t| opts.on("-t", "--target_path target_path", "path of the file to restore") do |t|
options[:target_path] = t options[:target_path] = t
end end
opts.on("-c", "--clientbucket_path clientbucket_path", "path to file to restore") do |c| opts.on("-c", "--clientbucket_path clientbucket_path", "where to restore from. Defaults to /var/lib/puppet/clientbucket") do |c|
options[:clientbucket_path] = c options[:clientbucket_path] = c
end end
end
parser.parse! opts.on_tail("-h", "--help", "Show this message") do
puts opts
if options[:target_path] == nil exit
print 'Enter target path: '
options[:target_path] = gets.chomp
end end
end.parse!
if options[:clientbucket_path] == nil if options[:clientbucket_path] == nil
print 'Enter clientbucket path: ' # We are keeping it on old path, as there are installations that have not moved on
options[:clientbucket_path] = gets.chomp # to Puppet 4
options[:clientbucket_path] = "/var/lib/puppet/clientbucket"
end end
target_path = options[:target_path]
clientbucket_path = options[:clientbucket_path]
available_files = Array.new available_files = Array.new
begin begin
Find.find(clientbucket_path) do |file_path| Find.find(options[:clientbucket_path]) do |file_path|
# Skip directories and "contents" files # Skip directories and "contents" files
next if FileTest.directory?(file_path) next if FileTest.directory?(file_path)
@ -45,7 +41,7 @@ begin
# See if this file has a path the user was looking for # See if this file has a path the user was looking for
path = File.open(file_path).first path = File.open(file_path).first
path.chomp! path.chomp!
if target_path == path if options[:target_path] == path
# Get the md5 string the file is referred to by # Get the md5 string the file is referred to by
file_path =~ /([^\/]+)\/paths$/; file_path =~ /([^\/]+)\/paths$/;
@ -64,11 +60,11 @@ begin
end end
rescue rescue
puts "Unable to open file path #{clientbucket_path}" puts "Unable to open file path #{options[:clientbucket_path]}"
end end
# See if we found any files for the user # See if we found any files for the user
if available_files.length == 0 if available_files.length == 0
puts "No files with path #{target_path} exist in the clientbucket" puts "No files with path #{options[:target_path]} exist in the clientbucket"
exit 2 exit 2
end end
@ -125,16 +121,16 @@ while true
when "v" when "v"
system("vim #{available_files[number][:contents_path]}") system("vim #{available_files[number][:contents_path]}")
when "d" when "d"
system("diff #{available_files[number][:contents_path]} #{target_path}") system("diff #{available_files[number][:contents_path]} #{options[:target_path]}")
when "u" when "u"
system("diff -u #{available_files[number][:contents_path]} #{target_path}") system("diff -u #{available_files[number][:contents_path]} #{options[:target_path]}")
when "r" when "r"
print "Restore to (default is to restore to #{target_path}): " print "Restore to (default is to restore to #{options[:target_path]}): "
choice = gets.strip choice = gets.strip
choice.chomp! choice.chomp!
# If not specified, use the default # If not specified, use the default
restore_path = target_path restore_path = options[:target_path]
if choice != '' if choice != ''
restore_path = choice restore_path = choice
end end