class Pwl::Commands::Base

Public Class Methods

default_locker_file() click to toggle source
# File lib/pwl/commands/base.rb, line 32
def default_locker_file
  File.expand_path("~/.#{program(:name)}.pstore")
end
exit_codes_help() click to toggle source
# File lib/pwl/commands/base.rb, line 28
def exit_codes_help
  EXIT_CODES.values.sort{|l,r| l.exit_code <=> r.exit_code}.collect{|m| "      #{m.exit_code.to_s.rjust(EXIT_CODES.size.to_s.size)}: #{m.to_s}"}.join("\n")
end

Protected Instance Methods

attr!(entry, field) click to toggle source

Returns the value of the passed attribute name if it is allowed to be retrieved from a locker entry

# File lib/pwl/commands/base.rb, line 92
def attr!(entry, field)
  raise InacessibleFieldError.new(field) unless entry.instance_variable_defined?("@#{field}".to_sym)
  entry.send(field)
end
exit_with(error_code, verbose, msg_args = {}) click to toggle source
# File lib/pwl/commands/base.rb, line 66
def exit_with(error_code, verbose, msg_args = {})
  msg = EXIT_CODES[error_code]
  raise "No message defined for error #{error_code}" if !msg

  if msg.error? || verbose # always print errors; messages only when verbose
    msg msg.to_s(msg_args)
  end

  exit(msg.exit_code)
end
get_password(prompt, gui = false) click to toggle source
# File lib/pwl/commands/base.rb, line 77
def get_password(prompt, gui = false)
  (gui ? Pwl::Dialog::Password.new(program(:name), prompt) : Pwl::Dialog::ConsolePasswordDialog.new(prompt)).get_input
end
get_text(prompt, gui = false) click to toggle source
# File lib/pwl/commands/base.rb, line 81
def get_text(prompt, gui = false)
  (gui ? Pwl::Dialog::Text.new(program(:name), prompt) : Pwl::Dialog::ConsoleTextDialog.new(prompt)).get_input
end
locker_file(options, init = false) click to toggle source
# File lib/pwl/commands/base.rb, line 39
def locker_file(options, init = false)
  result = options.file || self.class.default_locker_file

  if File.exists?(result) || init
    result
  else
    exit_with(:file_not_found, options.verbose, :file => result)
  end
end
msg(str) click to toggle source
# File lib/pwl/commands/base.rb, line 62
def msg(str)
  STDERR.puts("#{program(:name)}: #{str}")
end
new_locker(options, master_password) click to toggle source
# File lib/pwl/commands/base.rb, line 57
def new_locker(options, master_password)
  # Remote init not allowed. Or maybe it should be?
  Locker.new(locker_file(options, true), master_password, {:force => options.force})
end
open_locker(options, master_password = nil) click to toggle source
# File lib/pwl/commands/base.rb, line 49
def open_locker(options, master_password = nil)
  # TODO Use DRb at options.url if not nil
  locker_file = locker_file(options)
  msg "Attempting to open locker at #{locker_file}" if options.verbose

  Locker.open(locker_file, master_password || get_password("Enter the master password for #{program(:name)}:", options.gui))
end
validate!(pwd) click to toggle source
# File lib/pwl/commands/base.rb, line 85
def validate!(pwd)
  Pwl::Locker.password_policy.validate!(pwd)
end