Class: KyleCommands
- Inherits:
-
Object
- Object
- KyleCommands
- Defined in:
- lib/kyle_commands.rb
Overview
Command line access to Kyle
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
Instance Method Summary collapse
- #account ⇒ Object
- #animals ⇒ Object
- #ask_for_key ⇒ Object
- #batch? ⇒ Boolean
- #batch_generate ⇒ Object
- #check? ⇒ Boolean
- #file ⇒ Object
- #hostname ⇒ Object
-
#initialize(args = []) ⇒ KyleCommands
constructor
A new instance of KyleCommands.
- #key ⇒ Object
- #kyle_r_path ⇒ Object
- #main_args ⇒ Object
- #make_passwords(hostname, account, port, key) ⇒ Object
- #port ⇒ Object
- #record ⇒ Object
- #record? ⇒ Boolean
- #run ⇒ Object
- #saved_records ⇒ Object
- #single_by_choose? ⇒ Boolean
- #single_generate ⇒ Object
- #single_generate_choose ⇒ Object
Constructor Details
#initialize(args = []) ⇒ KyleCommands
Returns a new instance of KyleCommands.
7 8 9 |
# File 'lib/kyle_commands.rb', line 7 def initialize(args = []) @args = args end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
5 6 7 |
# File 'lib/kyle_commands.rb', line 5 def args @args end |
Instance Method Details
#account ⇒ Object
129 130 131 |
# File 'lib/kyle_commands.rb', line 129 def account @account ||= main_args[1] || ask('Account:') end |
#animals ⇒ Object
137 138 139 140 141 142 |
# File 'lib/kyle_commands.rb', line 137 def animals animal = batch? ? main_args[1] : main_args[3] return Constants::ANIMALS if animal.nil? Constants::ANIMALS.select { |a| a.downcase == animal.downcase } end |
#ask_for_key ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kyle_commands.rb', line 20 def ask_for_key return ask('Key:') { |q| q.echo = false } unless check? key = 'a' key_check = 'b' while key != key_check key = ask('Key:') { |q| q.echo = false } key_check = ask('Key (again):') { |q| q.echo = false } puts 'Keys do not match!' unless key == key_check end key end |
#batch? ⇒ Boolean
113 114 115 |
# File 'lib/kyle_commands.rb', line 113 def batch? args.include? '-b' end |
#batch_generate ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/kyle_commands.rb', line 36 def batch_generate text = File.open(file).read text.gsub!(/\r\n?/, "\n") text.each_line do |line| hostname, account, port = line.split(';') port.gsub!(/\n/, '') passwords = make_passwords(hostname, account, port, key) passwords.select { |a, _| animals.include? a }.each do |a, p| puts "#{hostname}:#{account}:#{port} (#{a}) = #{p}" end end end |
#check? ⇒ Boolean
109 110 111 |
# File 'lib/kyle_commands.rb', line 109 def check? (args.include? '-c') || (record?) end |
#file ⇒ Object
151 152 153 154 |
# File 'lib/kyle_commands.rb', line 151 def file fail 'File only for batch operations' unless batch? main_args[0] end |
#hostname ⇒ Object
125 126 127 |
# File 'lib/kyle_commands.rb', line 125 def hostname @hostname ||= main_args[0] || ask('Hostname:') end |
#key ⇒ Object
156 157 158 |
# File 'lib/kyle_commands.rb', line 156 def key @key ||= ask_for_key end |
#kyle_r_path ⇒ Object
54 55 56 |
# File 'lib/kyle_commands.rb', line 54 def kyle_r_path File.join(Dir.home, '.kyle') end |
#main_args ⇒ Object
121 122 123 |
# File 'lib/kyle_commands.rb', line 121 def main_args args.reject { |arg| arg[0] == '-' } end |
#make_passwords(hostname, account, port, key) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/kyle_commands.rb', line 144 def make_passwords(hostname, account, port, key) puts 'Generating...' puts '' Kyle.new(hostname, account, port, key).passwords end |
#port ⇒ Object
133 134 135 |
# File 'lib/kyle_commands.rb', line 133 def port @port ||= main_args[2] || ask('Port:') end |
#record ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/kyle_commands.rb', line 58 def record line_to_add = "#{hostname};#{account};#{port}" File.open(kyle_r_path, 'a') do |file| file.puts line_to_add end end |
#record? ⇒ Boolean
105 106 107 |
# File 'lib/kyle_commands.rb', line 105 def record? args.include? '-r' end |
#run ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/kyle_commands.rb', line 11 def run puts 'Kyle - A password manager for paranoids. ( 0.0.5 )' puts '' batch_generate if batch? single_generate_choose if single_by_choose? single_generate if !batch? && !single_by_choose? end |
#saved_records ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/kyle_commands.rb', line 66 def saved_records recs = [] i_a = 0 File.open(kyle_r_path).each do |line| recs << line.rstrip! puts "#{i_a} - #{line}" i_a += 1 end recs end |
#single_by_choose? ⇒ Boolean
117 118 119 |
# File 'lib/kyle_commands.rb', line 117 def single_by_choose? args.include? '-a' end |
#single_generate ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/kyle_commands.rb', line 93 def single_generate record if record? passwords = make_passwords(hostname, account, port, key) if animals.length > 1 Constants::ANIMALS.each { |a| puts "#{a}\t#{passwords[a]}" } else puts passwords[animals[0]] end end |
#single_generate_choose ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/kyle_commands.rb', line 78 def single_generate_choose recs = saved_records puts('') idx = ask('Selection:') r = recs[idx.to_i].split(';') @hostname = r[0] @account = r[1] @port = r[2] single_generate end |