Class: Keybox::Application::PasswordGenerator
- Defined in:
- lib/keybox/application/password_generator.rb
Constant Summary collapse
- ALGORITHMS =
{ "random" => :random, "pronounceable" => :pronounceable }
- SYMBOL_SETS =
Keybox::SymbolSet::MAPPING.keys
Instance Attribute Summary
Attributes inherited from Base
#error_message, #highline, #options, #parsed_options, #stderr, #stdin, #stdout
Instance Method Summary collapse
- #create_generator ⇒ Object
- #default_options ⇒ Object
- #option_parser ⇒ Object
- #options_to_symbol_sets(args) ⇒ Object
- #run ⇒ Object
Methods inherited from Base
#configuration_file_options, #error_version_help, #initialize, #merge_options, #set_io
Constructor Details
This class inherits a constructor from Keybox::Application::Base
Instance Method Details
#create_generator ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/keybox/application/password_generator.rb', line 100 def create_generator case @options.algorithm when :pronounceable generator = Keybox::CharGramGenerator.new when :random generator = Keybox::SymbolSetGenerator.new(@options.use_symbols) @options.require_symbols.each do |req| generator.required_sets << req end end generator.max_length = [@options.min_length,@options.max_length].max generator.min_length = [@options.min_length,@options.max_length].min # record what we set the generator to @options.max_length = generator.max_length @options.min_length = generator.min_length return generator end |
#default_options ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/keybox/application/password_generator.rb', line 77 def = OpenStruct.new .debug = 0 .show_version = false .show_help = false .algorithm = :random .number_to_generate = 6 .min_length = 8 .max_length = 10 .use_symbols = (["all"]) .require_symbols = ([]) return end |
#option_parser ⇒ Object
17 18 19 20 21 22 23 24 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 |
# File 'lib/keybox/application/password_generator.rb', line 17 def option_parser OptionParser.new do |op| op.separator "" op.separator "Options:" op.on("-aALGORITHM", "--algorithm ALGORITHM", ALGORITHMS.keys, "Select the algorithm for password ", " generation (#{ALGORITHMS.keys.join(', ')})") do |alg| key = ALGORITHMS.keys.find { |x| x =~ /^#{alg}/ } @parsed_options.algorithm = ALGORITHMS[key] end op.on("-h", "--help") do @parsed_options.show_help = true end op.on("-mLENGTH ", "--min-length LENGTH", Integer, "Minimum LENGTH of the new password"," in letters") do |len| @parsed_options.min_length = len end op.on("-xLENGTH ", "--max-length LENGTH", Integer, "Maximum LENGTH of the new password"," in letters") do |len| @parsed_options.max_length = len end op.on("-nNUMER", "--number NUMBER", Integer, "Generate NUMBER of passwords (default 6)") do |n| @parsed_options.number_to_generate = n end op.on("-uLIST", "--use symbol,set,list", Array, "Use only one ore more of the following", " symbol sets:", " [#{SYMBOL_SETS.join(', ')}]") do |list| list.each do |symbol_set| sym = SYMBOL_SETS.find { |s| s =~ /^#{symbol_set}/ } raise OptionParser::InvalidArgument, ": #{symbol_set} does not match any of #{SYMBOL_SETS.join(', ')}" if sym.nil? end @parsed_options.use_symbols = (list) end op.on("-rLIST","--require symbol,set,list", Array, "Require passwords to have letters from", " one or more of the following", " symbol sets:", " [#{SYMBOL_SETS.join(', ')}]") do |list| list.each do |symbol_set| sym = SYMBOL_SETS.find { |s| s =~ /^#{symbol_set}/ } raise OptionParser::InvalidArgument, ": #{symbol_set} does not match any of #{SYMBOL_SETS.join(', ')}" if sym.nil? end @parsed_options.require_symbols = (list) end op.on("-v", "--version", "Show version information") do @parsed_options.show_version = true end end end |
#options_to_symbol_sets(args) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/keybox/application/password_generator.rb', line 91 def (args) sets = [] args.each do |a| sym = SYMBOL_SETS.find { |s| s =~ /^#{a}/ } sets << Keybox::SymbolSet::MAPPING[sym] end sets end |
#run ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/keybox/application/password_generator.rb', line 121 def run error_version_help generator = create_generator @options.number_to_generate.times do @stdout.puts generator.generate end end |