class Searchpass::CLI

Constants

CREDENTIALS_FILE
CREDENTIALS_SEPARATOR
PROGRAM_NAME

Public Class Methods

run!(args) click to toggle source
# File lib/searchpass/cli.rb, line 12
def self.run!(args)
  @options = OpenStruct.new
  @options.case_sensitive = false
  @options.exact = false
  @opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{PROGRAM_NAME} [options] term [term2] ... [termN]"
    opts.program_name = PROGRAM_NAME
    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-c", "--case", "Perform case sensitive matching") do |v|
      @options.case_sensitive = v
    end

    opts.on("-e", "--exact", "Perform exact matching") do |v|
      @options.exact = v
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    opts.on_tail("--version", "Show version") do
      puts Searchpass::VERSION
      exit
    end
  end
  @opt_parser.parse!(args)
  if args.empty?
    puts "No search terms given"
    puts "See -h or --help for usage"
    exit(1)
  end
  perform_search(args)
rescue OptionParser::InvalidOption => e
  puts e.message
  puts "See -h or --help for usage"
  exit(1)
rescue => e
  puts "ERROR: #{e.message}"
  exit(1)
end

Protected Class Methods

credentials() click to toggle source
# File lib/searchpass/cli.rb, line 122
def self.credentials
  @credentials ||= read_credentials_file
end
output_credential(credential) click to toggle source
# File lib/searchpass/cli.rb, line 95
def self.output_credential(credential)
  puts "\nVendor:   #{credential['Vendor']}"
  puts "Name:     #{credential['Name']}"
  if credential.key?("Version") && !credential["Version"].empty?
    puts "Version:  #{credential['Version']}"
  end
  if credential.key?("Method") && !credential["Method"].empty?
    puts "Method:   #{credential['Method']}"
  end
  if credential.key?("User ID") && !credential["User ID"].empty?
    puts "User ID:  #{credential['User ID']}"
  end
  if credential.key?("Password") && !credential["Password"].empty?
    puts "Password: #{credential['Password']}"
  end
  if credential.key?("Level") && !credential["Level"].empty?
    puts "Level:    #{credential['Level']}"
  end
  if credential.key?("Doc") && !credential["Doc"].empty?
    puts "Doc:      #{credential['Doc']}"
  end
  if credential.key?("Notes") && !credential["Notes"].empty?
    puts "Notes:    #{credential['Notes']}"
  end
  puts "\n#{CREDENTIALS_SEPARATOR}\n"
end
read_credentials_file() click to toggle source
# File lib/searchpass/cli.rb, line 126
def self.read_credentials_file
  if !File.exists?(CREDENTIALS_FILE)
    fail CredentialsFileNotReadable, "Credentials file does not exist"
  end

  if !File.readable?(CREDENTIALS_FILE)
    fail CredentialsFileNotReadable, "Credentials file is not readable"
  end
  credentials = JSON.parse(File.read(CREDENTIALS_FILE)).tap do |c|
    if !c.is_a?(Array)
      fail CredentialsFileCorrupt, "Credentials file is invalid"
    end
  end
rescue JSON::ParserError
  raise CredentialsFileCorrupt, "Credentials file contains invalid JSON"
end