Class: KyleCommands

Inherits:
Object
  • Object
show all
Defined in:
lib/kyle_commands.rb

Overview

Command line access to Kyle

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#argsObject

Returns the value of attribute args.



5
6
7
# File 'lib/kyle_commands.rb', line 5

def args
  @args
end

Instance Method Details

#accountObject



129
130
131
# File 'lib/kyle_commands.rb', line 129

def 
  @account ||= main_args[1] || ask('Account:')
end

#animalsObject



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_keyObject



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

Returns:

  • (Boolean)


113
114
115
# File 'lib/kyle_commands.rb', line 113

def batch?
  args.include? '-b'
end

#batch_generateObject



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, , port = line.split(';')

    port.gsub!(/\n/, '')

    passwords = make_passwords(hostname, , port, key)

    passwords.select { |a, _| animals.include? a }.each do |a, p|
      puts "#{hostname}:#{account}:#{port} (#{a}) = #{p}"
    end
  end
end

#check?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/kyle_commands.rb', line 109

def check?
  (args.include? '-c') || (record?)
end

#fileObject



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

#hostnameObject



125
126
127
# File 'lib/kyle_commands.rb', line 125

def hostname
  @hostname ||= main_args[0] || ask('Hostname:')
end

#keyObject



156
157
158
# File 'lib/kyle_commands.rb', line 156

def key
  @key ||= ask_for_key
end

#kyle_r_pathObject



54
55
56
# File 'lib/kyle_commands.rb', line 54

def kyle_r_path
  File.join(Dir.home, '.kyle')
end

#main_argsObject



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, , port, key)
  puts 'Generating...'
  puts ''

  Kyle.new(hostname, , port, key).passwords
end

#portObject



133
134
135
# File 'lib/kyle_commands.rb', line 133

def port
  @port ||= main_args[2] || ask('Port:')
end

#recordObject



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

Returns:

  • (Boolean)


105
106
107
# File 'lib/kyle_commands.rb', line 105

def record?
  args.include? '-r'
end

#runObject



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_recordsObject



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

Returns:

  • (Boolean)


117
118
119
# File 'lib/kyle_commands.rb', line 117

def single_by_choose?
  args.include? '-a'
end

#single_generateObject



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, , port, key)

  if animals.length > 1
    Constants::ANIMALS.each { |a| puts "#{a}\t#{passwords[a]}" }
  else
    puts passwords[animals[0]]
  end
end

#single_generate_chooseObject



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