Class: Lockr
- Inherits:
-
Object
- Object
- Lockr
- Defined in:
- lib/lockr.rb
Instance Method Summary collapse
- #acquire_additional_input(options) ⇒ Object
- #merge_config(options) ⇒ Object
- #parse_options ⇒ Object
- #process_actions(options) ⇒ Object
- #run ⇒ Object
- #validate_options(options) ⇒ Object
Instance Method Details
#acquire_additional_input(options) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/lockr.rb', line 119 def acquire_additional_input( ) # id is required for all actions except list while [:id].nil? and %w{ l list}.index( [:action]).nil? [:id] = ask("Id? ") { |q| } [:id] = nil if [:id].strip() == '' end # username is required for actions add, remove actions_requiring_username = %w{ a add r remove} while [:username].nil? and not actions_requiring_username.index( [:action]).nil? [:username] = ask("Username? ") { |q| } [:username] = nil if [:username].strip == '' end # url is optional for add actions_requiring_url = %w{ a add} if [:url].nil? and not actions_requiring_url.index( [:action]).nil? [:url] = ask("Url? ") { |q| } [:url] = nil if [:url].strip() == '' end end |
#merge_config(options) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/lockr.rb', line 85 def merge_config( ) configfile = Configuration.new() if configfile.config.nil? return end unless configfile.config.has_key?( :lockr) return end cfg = configfile.config[:lockr] [:vault] = File.(cfg[:vault]) if [:vault] == 'vault.yaml' end |
#parse_options ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lockr.rb', line 25 def () = {} optparse = OptionParser.new do|opts| # Set a banner, displayed at the top # of the help screen. opts. = "Usage: lockr.rb [options]" # Define the options, and what they do [:action] = nil opts.on( '-a', '--action ACTION', 'Execute the requested ACTION (add, remove, list, show)' ) do |id| [:action] = id end [:id] = nil opts.on( '-i', '--id ID', 'the ID of the password set' ) do |id| [:id] = id end [:keyfile] = nil opts.on( '-k', '--keyfile FILE', 'the FILE to use as key for the password encryption') do |file| [:keyfile] = file end [:vault] = 'vault.yaml' opts.on( '-v', '--vault FILE', 'FILE is the name of the vault to store the password sets') do |file| [:vault] = file end [:generatepwd] = nil opts.on( '-g', '--genpwd PARAMS', 'generate a random password (based on the optional PARAMS)') do |params| [:generatepwd] = params end # This displays the help screen, all programs are # assumed to have this option. opts.on( '-h', '--help', 'Display this screen' ) do puts opts exit end opts.on('-v', '--version', 'Show version') do puts "Lockr #{LockrVer::VERSION} (#{LockrVer::DATE})" exit end opts.separator "" opts.separator "For detailed instructions on how to use Lockr, please visit http://lockr.byteblues.com" end # Parse the command-line. Remember there are two forms # of the parse method. The 'parse' method simply parses # ARGV, while the 'parse!' method parses ARGV and removes # any options found there, as well as any parameters for # the options. What's left is the list of files to resize. optparse.parse! end |
#process_actions(options) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/lockr.rb', line 141 def process_actions( ) begin case [:action] when 'a', 'add' if [:generatepwd].nil? password = ask("Password? ") { |q| q.echo = "x" } else password = PasswordGenerator.new.generate( [:generatepwd]) end action = AddAction.new( [:id], [:url], [:username], password, [:keyfile], [:vault]) when 'r', 'remove' action = RemoveAction.new( [:id], [:username], [:keyfile], [:vault]) when 's', 'show' action = ShowAction.new( [:id], [:username], [:keyfile], [:vault]) when 'l', 'list' action = ListAction.new( [:keyfile], [:vault]) else puts "Unknown action #{options[:action]}" end rescue OpenSSL::Cipher::CipherError say( "<%= color('Invalid keyfile', :red) %>") exit 42 end end |
#run ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/lockr.rb', line 17 def run() = () merge_config( ) ( ) acquire_additional_input( ) process_actions( ) end |
#validate_options(options) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/lockr.rb', line 100 def ( ) if [:action].nil? puts 'Please provide an action (--action)' exit 1 end allowed_actions = %w{ a add r remove s show l list} if allowed_actions.index( [:action]).nil? puts "Allowed actions are add, remove, list and show" exit 2 end # keyfile is required for all actions other than list if [:keyfile].nil? and %w{ l list}.index( [:action]).nil? puts 'Please provide an encryption key file (--keyfile)' exit 3 end end |