diff --git a/README.md b/README.md index 3cc4491..9f85ffb 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,19 @@ or more of these older versions. 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: - # clientbucket.rb /some/test/file + # clientbucket.rb -t /some/test/file [0]: 41f59421026a473a0378c58d539069c6 Thu Feb 09 15:56:36 +0000 2012 [1]: 6bc6ab38660066ea8cf0743b889bd075 Mon Feb 20 15:23:59 +0000 2012 [2]: 74da6d605bfd7ecad38904bc35a0292a Thu May 24 13:45:10 +0100 2012 diff --git a/clientbucket.rb b/clientbucket.rb index 5987409..e29980e 100644 --- a/clientbucket.rb +++ b/clientbucket.rb @@ -1,42 +1,38 @@ -#!/usr/bin/ruby +#!/usr/bin/env ruby require 'rubygems' require 'find' require 'optparse' +ARGV << '-h' if ARGV.empty? 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.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 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 end -end -parser.parse! - -if options[:target_path] == nil - print 'Enter target path: ' - options[:target_path] = gets.chomp -end + opts.on_tail("-h", "--help", "Show this message") do + puts opts + exit + end +end.parse! if options[:clientbucket_path] == nil - print 'Enter clientbucket path: ' - options[:clientbucket_path] = gets.chomp + # We are keeping it on old path, as there are installations that have not moved on + # to Puppet 4 + options[:clientbucket_path] = "/var/lib/puppet/clientbucket" end -target_path = options[:target_path] - -clientbucket_path = options[:clientbucket_path] - available_files = Array.new begin - Find.find(clientbucket_path) do |file_path| + Find.find(options[:clientbucket_path]) do |file_path| # Skip directories and "contents" files next if FileTest.directory?(file_path) @@ -45,7 +41,7 @@ begin # See if this file has a path the user was looking for path = File.open(file_path).first path.chomp! - if target_path == path + if options[:target_path] == path # Get the md5 string the file is referred to by file_path =~ /([^\/]+)\/paths$/; @@ -64,11 +60,11 @@ begin end rescue - puts "Unable to open file path #{clientbucket_path}" + puts "Unable to open file path #{options[:clientbucket_path]}" end # See if we found any files for the user 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 end @@ -125,16 +121,16 @@ while true when "v" system("vim #{available_files[number][:contents_path]}") when "d" - system("diff #{available_files[number][:contents_path]} #{target_path}") + system("diff #{available_files[number][:contents_path]} #{options[:target_path]}") 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" - 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.chomp! # If not specified, use the default - restore_path = target_path + restore_path = options[:target_path] if choice != '' restore_path = choice end