Class: Pwl::Commands::Init

Inherits:
Base
  • Object
show all
Defined in:
lib/pwl/commands/init.rb

Instance Method Summary collapse

Methods inherited from Base

default_locker_file, exit_codes_help

Instance Method Details

#call(args, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pwl/commands/init.rb', line 4

def call(args, options)
  locker_file = locker_file(options, true)
  msg "Attempting to initialize new locker at #{locker_file}" if options.verbose

  # Locker checks this too, but we want to fail fast.
  exit_with(:is_dir, options.verbose, :file => locker_file) if File.exists?(locker_file) && File.directory?(locker_file)
  exit_with(:file_exists, options.verbose, :file => locker_file) if File.exists?(locker_file) && !options.force

  begin
    begin
      master_password = get_password('Enter new master password:', options.gui)
    end while begin
      validate!(master_password) # Basic idea from http://stackoverflow.com/questions/136793/is-there-a-do-while-loop-in-ruby
    rescue Pwl::InvalidMasterPasswordError => e
      msg e.message
      options.gui || STDIN.tty? # only continue the loop when in interactive mode
    end

    # Ask for password confirmation if running in interactive mode (terminal)
    if STDIN.tty? && master_password != get_password('Enter master password again:', options.gui)
      exit_with(:passwords_dont_match, options.verbose)
    end
  rescue Pwl::Dialog::Cancelled
    exit_with(:aborted, options.verbose)
  end

  new_locker(options, master_password)
  msg "Successfully initialized new locker at #{locker_file}" if options.verbose
end