Class: Keyrack::UI::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/keyrack/ui/console.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



6
7
8
9
# File 'lib/keyrack/ui/console.rb', line 6

def initialize
  @highline = HighLine.new
  @mode = :copy
end

Instance Attribute Details

#modeObject

Returns the value of attribute mode.



4
5
6
# File 'lib/keyrack/ui/console.rb', line 4

def mode
  @mode
end

Instance Method Details

#change_username(old_username) ⇒ Object



190
191
192
193
194
# File 'lib/keyrack/ui/console.rb', line 190

def change_username(old_username)
  colored_old_username = @highline.color(old_username, :cyan)
  @highline.say("Current username: #{colored_old_username}")
  @highline.ask("New username (blank to cancel): ") { |q| q.validate = /\S/ }.to_s
end

#choose_entry_to_edit(group) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/keyrack/ui/console.rb', line 151

def choose_entry_to_edit(group)
  choices = {'c' => :cancel}
  entry_choices = print_entries({
    :group => group,
    :title => "Choose entry"
  })
  choices.update(entry_choices)

  @highline.say("c. Cancel")

  answer = @highline.ask("? ") { |q| q.in = choices.keys }.to_s
  result = choices[answer]
  if result == :cancel
    nil
  else
    result
  end
end

#confirm_delete_entry(site) ⇒ Object



201
202
203
204
# File 'lib/keyrack/ui/console.rb', line 201

def confirm_delete_entry(site)
  entry_name = @highline.color("#{site.name} [#{site.username}]", :red)
  @highline.agree("You're about to delete #{entry_name}. Are you sure? [yn] ")
end

#confirm_overwrite_entry(site) ⇒ Object



196
197
198
199
# File 'lib/keyrack/ui/console.rb', line 196

def confirm_overwrite_entry(site)
  entry_name = @highline.color("#{site.name} [#{site.username}]", :cyan)
  @highline.agree("There's already an entry for: #{entry_name}. Do you want to overwrite it? [yn] ")
end

#display_first_time_noticeObject



117
118
119
# File 'lib/keyrack/ui/console.rb', line 117

def display_first_time_notice
  @highline.say("This looks like your first time using Keyrack.  I'll need to ask you a few questions first.")
end

#display_invalid_password_noticeObject



206
207
208
# File 'lib/keyrack/ui/console.rb', line 206

def display_invalid_password_notice
  @highline.say("Invalid password.")
end

#edit_entry(site) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/keyrack/ui/console.rb', line 170

def edit_entry(site)
  colored_entry = @highline.color("#{site.name} [#{site.username}]", :cyan)
  @highline.say("Editing entry: #{colored_entry}")
  @highline.say("u. Change username")
  @highline.say("p. Change password")
  @highline.say("d. Delete")
  @highline.say("c. Cancel")

  case @highline.ask("? ") { |q| q.in = %w{u p d c} }.to_s
  when "u"
    :change_username
  when "p"
    :change_password
  when "d"
    :delete
  when "c"
    nil
  end
end

#get_generated_passwordObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/keyrack/ui/console.rb', line 224

def get_generated_password
  password = nil
  loop do
    password = Utils.generate_password
    colored_password = @highline.color(password, :cyan)
    case @highline.ask("Generated #{colored_password}.  Sound good? [ync] ") { |q| q.in = %w{y n c} }.to_s
    when "y"
      break
    when "c"
      password = nil
      break
    end
  end
  password
end

#get_manual_passwordObject



240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/keyrack/ui/console.rb', line 240

def get_manual_password
  password = nil
  loop do
    password = @highline.ask("Password: ") { |q| q.echo = false }.to_s
    confirmation = @highline.ask("Password (again): ") { |q| q.echo = false }.to_s
    if password == confirmation
      break
    end
    @highline.say("Passwords didn't match. Try again!")
  end
  password
end

#get_new_entryObject



109
110
111
112
113
114
115
# File 'lib/keyrack/ui/console.rb', line 109

def get_new_entry
  result = {}
  result[:site]     = @highline.ask("Label: ").to_s
  result[:username] = @highline.ask("Username: ").to_s
  result[:password] = get_new_password
  result[:password].nil? ? nil : result
end

#get_new_group(options = {}) ⇒ Object



105
106
107
# File 'lib/keyrack/ui/console.rb', line 105

def get_new_group(options = {})
  @highline.ask("Group: ") { |q| q.validate = /^\w[\w\s]*$/ }.to_s
end

#get_new_passwordObject



210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/keyrack/ui/console.rb', line 210

def get_new_password
  result = nil
  case @highline.ask("Generate password? [ync] ") { |q| q.in = %w{y n c} }.to_s
  when "y"
    result = get_generated_password
    if result.nil?
      result = get_manual_password
    end
  when "n"
    result = get_manual_password
  end
  result
end

#get_passwordObject



11
12
13
# File 'lib/keyrack/ui/console.rb', line 11

def get_password
  @highline.ask("Keyrack password: ") { |q| q.echo = false }.to_s
end


15
16
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/keyrack/ui/console.rb', line 15

def menu(options)
  current_group = options[:group]
  dirty = options[:dirty]
  at_top = options[:at_top]
  open = options[:open]

  choices = {'n' => :new, 'q' => :quit, 'm' => :mode}
  entry_choices = print_entries({
    :group => current_group,
    :title => at_top ? "Keyrack Main Menu" : current_group.name,
    :open => open
  })
  choices.update(entry_choices)

  @highline.say("Mode: #{@mode}")
  commands = "Commands:"

  if at_top
    if open
      choices['c'] = :collapse
      commands << " [c]ollapse"
    else
      choices['o'] = :open
      commands << " [o]pen"
    end
  end

  commands << " [n]ew"

  if !current_group.sites.empty?
    choices['e'] = :edit
    commands << " [e]dit"
  end

  choices['g'] = :new_group
  commands << " [g]roup"

  if options[:enable_up]
    choices['u'] = :up
    commands << " [u]p"
  end

  if !at_top
    choices['t'] = :top
    commands << " [t]op"
  end

  if dirty
    choices['s'] = :save
    commands << " [s]ave"
  end
  commands << " [m]ode [q]uit"
  @highline.say(commands)

  answer = @highline.ask("? ") { |q| q.in = choices.keys }.to_s
  result = choices[answer]
  case result
  when Symbol
    if result == :quit && dirty && !@highline.agree("Really quit?  You have unsaved changes! [yn] ")
      nil
    elsif result == :mode
      @mode = @mode == :copy ? :print : :copy
      nil
    else
      result
    end
  when Hash
    if result.has_key?(:group)
      {:group => current_group.group(result[:group])}
    else
      password = result[:site].password

      if @mode == :copy
        Clipboard.copy(password)
        @highline.say("The password has been copied to your clipboard.")
      elsif @mode == :print
        password = @highline.color(password, :cyan)
        @highline.ask("Here you go: #{password}. Done? ") do |question|
          question.echo = false
          if HighLine::SystemExtensions::CHARACTER_MODE != 'stty'
            question.character = true
            question.overwrite = true
          end
        end
      end
      nil
    end
  end
end

#password_setupObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/keyrack/ui/console.rb', line 121

def password_setup
  password = confirmation = nil
  loop do
    password = @highline.ask("New passphrase: ") { |q| q.echo = false }.to_s
    confirmation = @highline.ask("Confirm passphrase: ") { |q| q.echo = false }.to_s
    break if password == confirmation
    @highline.say("Passphrases didn't match.")
  end
  password
end

#store_setupObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/keyrack/ui/console.rb', line 132

def store_setup
  result = {}
  result['type'] = @highline.choose do |menu|
    menu.header = "Choose storage type"
    menu.choices("filesystem", "ssh")
  end

  case result['type']
  when 'filesystem'
    result['path'] = 'database'
  when 'ssh'
    result['host'] = @highline.ask("Host: ").to_s
    result['user'] = @highline.ask("User: ").to_s
    result['path'] = @highline.ask("Remote path: ").to_s
  end

  result
end