Class: SafeUtils
- Inherits:
-
Object
- Object
- SafeUtils
- Defined in:
- lib/safeutils.rb
Overview
SafeUtils Utility methods for the Safe application
Class Method Summary collapse
-
.diff(safe_file, dir) ⇒ Object
Diffs two .safe.xml files.
- .do_merge(safe_file, diffs, override_master = false) ⇒ Object
-
.get_diffs(safe_file, other_file) ⇒ Object
Returns the SafeDiff objects in an Array.
-
.get_password(prompt = 'Password: ', mask = '*') ⇒ Object
Prompts the user for a password.
-
.get_safe_dir(var = 'SAFE_DIR') ⇒ Object
Gets the directory for the data file.
- .get_safe_file(dir, prompt = 'Password: ') ⇒ Object
-
.import(safe_file, import_file) ⇒ Object
Imports a file in the format: name,ID,password name,ID,password etc.
-
.merge(safe_file, dir) ⇒ Object
Merges two .safe.xml files.
- .replace_master?(diff) ⇒ Boolean
Class Method Details
.diff(safe_file, dir) ⇒ Object
Diffs two .safe.xml files
87 88 89 90 91 |
# File 'lib/safeutils.rb', line 87 def SafeUtils.diff(safe_file, dir) other_file = get_safe_file(dir, "#{dir} Password: ") diffs = get_diffs(safe_file, other_file) puts diffs end |
.do_merge(safe_file, diffs, override_master = false) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/safeutils.rb', line 125 def SafeUtils.do_merge(safe_file, diffs, override_master = false) diffs.each do |diff| case diff.operation when SafeDiff::OTHER then safe_file.insert(diff.other.name, diff.other.id, diff.other.password) when SafeDiff::CHANGE then # Update safe file if override_master || replace_master?(diff) safe_file.insert(diff.other.name, diff.other.id, diff.other.password) end end end end |
.get_diffs(safe_file, other_file) ⇒ Object
Returns the SafeDiff objects in an Array
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/safeutils.rb', line 102 def SafeUtils.get_diffs(safe_file, other_file) diffs = Array.new # Find master-only and changes safe_file.entries.each_value do |entry| if other_file.entries.has_key? entry.name other_entry = other_file.entries[entry.name] diffs << SafeDiff.new(entry, other_entry) unless entry.equals? other_entry else diffs << SafeDiff.new(entry, nil) end end # Find other-only other_file.entries.each_value do |entry| if !safe_file.entries.has_key? entry.name diffs << SafeDiff.new(nil, entry) end end diffs end |
.get_password(prompt = 'Password: ', mask = '*') ⇒ Object
Prompts the user for a password
55 56 57 |
# File 'lib/safeutils.rb', line 55 def SafeUtils.get_password(prompt = 'Password: ', mask = '*') ask(prompt) { |q| q.echo = mask } end |
.get_safe_dir(var = 'SAFE_DIR') ⇒ Object
Gets the directory for the data file
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/safeutils.rb', line 43 def SafeUtils.get_safe_dir(var = 'SAFE_DIR') dir = ENV[var] raise "Set environment variable #{var} to the directory for your safe file" unless dir begin d = Dir.new(dir) rescue raise "Environment variable #{var} does not point to an existing directory (#{dir})" end dir end |
.get_safe_file(dir, prompt = 'Password: ') ⇒ Object
81 82 83 84 |
# File 'lib/safeutils.rb', line 81 def SafeUtils.get_safe_file(dir, prompt = 'Password: ') password = SafeUtils::get_password(prompt) SafeFile.new(password, dir) end |
.import(safe_file, import_file) ⇒ Object
Imports a file in the format: name,ID,password name,ID,password etc.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/safeutils.rb', line 63 def SafeUtils.import(safe_file, import_file) begin open(import_file).each do |line| puts "Importing #{line}" fields = line.split(',') if fields.length == 3 safe_file.insert(fields[0], fields[1], fields[2]) else puts "Cannot import; number of fields = #{fields.length}" end end safe_file.save rescue puts $! raise "Problem importing file #{import_file}" end end |
.merge(safe_file, dir) ⇒ Object
Merges two .safe.xml files
94 95 96 97 98 99 |
# File 'lib/safeutils.rb', line 94 def SafeUtils.merge(safe_file, dir) other_file = get_safe_file(dir, "#{dir} Password: ") diffs = get_diffs(safe_file, other_file) do_merge safe_file, diffs safe_file.save end |
.replace_master?(diff) ⇒ Boolean
137 138 139 140 |
# File 'lib/safeutils.rb', line 137 def SafeUtils.replace_master?(diff) print "Replace:\n\t#{diff.master.to_s}\nWith:\n\t#{diff.other.to_s}\n?" gets.chomp == 'y' end |