did some work

This commit is contained in:
Tristan Ancelet 2024-05-24 19:13:55 -05:00
parent 1d546c93b1
commit 3de74425e5

View File

@ -14,12 +14,11 @@ Description:
search the filebucket and restore from it. search the filebucket and restore from it.
Actions: Actions:
search <term> : Search for bucket entries matching a portion of the filepath search <term> : Search for bucket entries matching a portion of the filepath
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,36 +323,76 @@ 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| end
entry.filepaths.each do |path|
if not filenames.include? path def get_entry_by_file (bucket, filenames)
filenames.push(path) filename = ""
end if filenames.count == 1
end 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]
entry = bucket.entries[$CONFIG[:search_term]]
filepath = ""
if entry.filepaths.count == 1
filepath = entry.filepaths[0]
else
filepath = get_selections entry.filepaths, "What filepath do you wish to restore to?"
end
if filepath[0] != '/'
filepath = "/#{filepath}"
end
return entry, filepath
else
puts "There were no entries corresponding to #{$CONFIG[:search_term]}"
end end
puts filenames.sort.join("\n")
end end
def restore_entry (bucket) def restore_entry (bucket)
if bucket.entries.has_key? $CONFIG[:search_term] entry = nil
entry = bucket.entries[$CONFIG[:search_term]] if bucket.filenames.any{ |filename| filename.include? $CONFIG[:search_term] }
if entry.filepaths.count == 1 filenames = bucket.filenames.select {|filename| filename.include $CONFIG[:search_term]}
filepath = entry.filepaths[0] entry, filepath = get_entry_by_file bucket, filenames
if filepath[0] != '/' else
filepath = "/#{filepath}" entry, filepath = get_entry_by_hash bucket
end end
if get_verification "Are you sure you want to overwrite #{filepath}?"
File.open(filepath,'w') do |file| if get_verification "Are you sure you want to overwrite #{filepath}?"
file.write(entry.content) File.open(filepath,'w') do |file|
end file.write(entry.content)
else
puts "Ok not overwriting."
end
else
end end
else else
puts "There were no entries corresponding to #{$CONFIG[:search_term]}" puts "Ok not overwriting."
end end
end end