Class: Pwl::Commands::Passwd

Inherits:
Base
  • Object
show all
Defined in:
lib/pwl/commands/passwd.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
33
34
35
36
37
38
39
# File 'lib/pwl/commands/passwd.rb', line 4

def call(args, options)
  begin
    locker = open_locker(options)

    if !STDIN.tty? && !options.gui
      # If we are in a pipe and do not run in GUI mode, we accept the new master password as args[0]
      new_master_password = args[0]

      begin
        validate!(new_master_password)
      rescue Pwl::InvalidMasterPasswordError => e
        exit_with(:validation_new_failed, options.verbose, :message => e.message)
      end
    else
      # If running interactively (console or gui), we loop until we get a valid password or break
      begin
        new_master_password = get_password("Enter the new master password for #{program(:name)}:", options.gui)
      end while begin
        validate!(new_master_password)
      rescue Pwl::InvalidMasterPasswordError => e
        msg e.message
        options.gui || STDIN.tty? # only continue the loop when in interactive mode
      end

      # Confirm new password
      if new_master_password != get_password("Enter the new master password for #{program(:name)} again:", options.gui)
        exit_with(:passwords_dont_match, options.verbose)
      end
    end
  rescue Pwl::Dialog::Cancelled
    exit_with(:aborted, options.verbose)
  end

  locker.change_password!(new_master_password)
  msg "Successfully changed master password for #{program(:name)}." if options.verbose
end