did some work
This commit is contained in:
parent
1d546c93b1
commit
3de74425e5
135
bucket-tool
135
bucket-tool
@ -18,8 +18,7 @@ Actions:
|
|||||||
list : List all Bucket entries
|
list : List all Bucket entries
|
||||||
list-files : List all files/paths that have been backed up to the bucket
|
list-files : List all files/paths that have been backed up to the bucket
|
||||||
get <entry-hash> : Get the content of a specific entry (by hash)
|
get <entry-hash> : Get the content of a specific entry (by hash)
|
||||||
restore-hash <entry-hash> : Restore previous state of file stored in bucket (by-hash)
|
restore <value> : Restore previous state of file stored in bucket. Value can be hash or filename/filepath
|
||||||
restore-file <filepath> : Restore previous state of file stored in bucket (by-filepath/filename)
|
|
||||||
|
|
||||||
Global Flags:
|
Global Flags:
|
||||||
-d | --debug : Set debug flag
|
-d | --debug : Set debug flag
|
||||||
@ -79,6 +78,55 @@ def get_verification (prompt = "Do you want to continue?")
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_selections (reference_array, prompt = "Which of the following do you want to select? ", options = { :multiple => false, }, &procedure)
|
||||||
|
## Making clone of array since the selections were passed by reference
|
||||||
|
selections = reference_array.clone
|
||||||
|
|
||||||
|
def put_prompt (selections, prompt)
|
||||||
|
puts prompt
|
||||||
|
selections.each_with_index do |value,index|
|
||||||
|
puts "#{index} : #{value}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_value(output_list, item, procedure)
|
||||||
|
value =
|
||||||
|
end
|
||||||
|
|
||||||
|
if options[:multiple] == true
|
||||||
|
output = Array.new
|
||||||
|
else
|
||||||
|
output = ""
|
||||||
|
end
|
||||||
|
|
||||||
|
put_prompt selections, prompt
|
||||||
|
|
||||||
|
while true
|
||||||
|
choice = Integer(STDIN.gets.strip())
|
||||||
|
if choice.is_a? Integer
|
||||||
|
if choice >= 0 and choice < selections.count
|
||||||
|
if options[:multiple] == true
|
||||||
|
output.push(selections[choice])
|
||||||
|
selections.delete_at(choice)
|
||||||
|
if get_verification "Are you done selecting?"
|
||||||
|
break
|
||||||
|
end
|
||||||
|
put_prompt selections, prompt
|
||||||
|
else
|
||||||
|
output = selections[choice]
|
||||||
|
break
|
||||||
|
end
|
||||||
|
else
|
||||||
|
puts "#{choice} is not between the values of 0 and #{selections.count}. Please try again."
|
||||||
|
end
|
||||||
|
else
|
||||||
|
puts "#{choice} is not a valid option. Please try again."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
output
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
# END: Helper Functions
|
# END: Helper Functions
|
||||||
|
|
||||||
|
|
||||||
@ -164,6 +212,7 @@ case $CONFIG[:action]
|
|||||||
usage
|
usage
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
## END: Checks
|
## END: Checks
|
||||||
@ -234,6 +283,18 @@ class Bucket
|
|||||||
end
|
end
|
||||||
log "Bucket[#{@bucketdir}] was loaded"
|
log "Bucket[#{@bucketdir}] was loaded"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def filenames
|
||||||
|
filenames = Array.new
|
||||||
|
@entries.each_value do |entry|
|
||||||
|
entry.filepaths.each do |path|
|
||||||
|
if not filenames.include? path
|
||||||
|
filenames.push(path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
filesnames
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# END: Classes
|
# END: Classes
|
||||||
@ -262,25 +323,70 @@ def list_all_entries (bucket)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def list_entry_files (bucket)
|
def list_entry_files (bucket)
|
||||||
filenames = Array.new
|
puts bucket.filenames.sort.join("\n")
|
||||||
bucket.entries.each_value do |entry|
|
|
||||||
entry.filepaths.each do |path|
|
|
||||||
if not filenames.include? path
|
|
||||||
filenames.push(path)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
puts filenames.sort.join("\n")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def restore_entry (bucket)
|
def get_entry_by_file (bucket, filenames)
|
||||||
|
filename = ""
|
||||||
|
if filenames.count == 1
|
||||||
|
filename = filenames[0]
|
||||||
|
else
|
||||||
|
filename = get_selections filenames, "Your filename matched multiple files. Please select one to restore"
|
||||||
|
end
|
||||||
|
|
||||||
|
entries = bucket.entries.each_value.select{|entry| entry.filepaths.include filename}
|
||||||
|
if entries.count == 1
|
||||||
|
entry = entries[0]
|
||||||
|
else
|
||||||
|
while true {
|
||||||
|
entry_timestamp_selection = get_selections entries.map{|entry| entry.mtime}, "Which timestamp to you want to revert the file to?" {|entry| entry.hash}
|
||||||
|
entries.each_value.select
|
||||||
|
entry =
|
||||||
|
|
||||||
|
if get_verification "Do you want to see the contents of #{filename} at this time?"
|
||||||
|
puts entry.content
|
||||||
|
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
if filename[0] != '/'
|
||||||
|
filename = "/#{filename}"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_entry_by_hash (bucket)
|
||||||
if bucket.entries.has_key? $CONFIG[:search_term]
|
if bucket.entries.has_key? $CONFIG[:search_term]
|
||||||
entry = bucket.entries[$CONFIG[:search_term]]
|
entry = bucket.entries[$CONFIG[:search_term]]
|
||||||
|
filepath = ""
|
||||||
if entry.filepaths.count == 1
|
if entry.filepaths.count == 1
|
||||||
filepath = entry.filepaths[0]
|
filepath = entry.filepaths[0]
|
||||||
|
else
|
||||||
|
filepath = get_selections entry.filepaths, "What filepath do you wish to restore to?"
|
||||||
|
end
|
||||||
|
|
||||||
if filepath[0] != '/'
|
if filepath[0] != '/'
|
||||||
filepath = "/#{filepath}"
|
filepath = "/#{filepath}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return entry, filepath
|
||||||
|
|
||||||
|
else
|
||||||
|
puts "There were no entries corresponding to #{$CONFIG[:search_term]}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def restore_entry (bucket)
|
||||||
|
entry = nil
|
||||||
|
if bucket.filenames.any{ |filename| filename.include? $CONFIG[:search_term] }
|
||||||
|
filenames = bucket.filenames.select {|filename| filename.include $CONFIG[:search_term]}
|
||||||
|
entry, filepath = get_entry_by_file bucket, filenames
|
||||||
|
else
|
||||||
|
entry, filepath = get_entry_by_hash bucket
|
||||||
|
end
|
||||||
|
|
||||||
if get_verification "Are you sure you want to overwrite #{filepath}?"
|
if get_verification "Are you sure you want to overwrite #{filepath}?"
|
||||||
File.open(filepath,'w') do |file|
|
File.open(filepath,'w') do |file|
|
||||||
file.write(entry.content)
|
file.write(entry.content)
|
||||||
@ -288,11 +394,6 @@ def restore_entry (bucket)
|
|||||||
else
|
else
|
||||||
puts "Ok not overwriting."
|
puts "Ok not overwriting."
|
||||||
end
|
end
|
||||||
else
|
|
||||||
end
|
|
||||||
else
|
|
||||||
puts "There were no entries corresponding to #{$CONFIG[:search_term]}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# END: Work Functions
|
# END: Work Functions
|
||||||
|
Loading…
Reference in New Issue
Block a user