class PerfectWorld::Cli

Constants

CONFIG

Default config.

Public Class Methods

new(config = {}) click to toggle source

Sets the config.

# File lib/perfect_world/cli.rb, line 21
def initialize(config = {})
  @config = CONFIG.merge(config)
  GPGME::Engine.home_dir = @config['gpgdir'] if @config.key?('gpgdir')
end

Public Instance Methods

run(commands) click to toggle source

Loads the database and executes the provided commands.

# File lib/perfect_world/cli.rb, line 27
def run(commands)
  DB.open(@config.fetch('database'), @config['owner']) do |db|
    commands.each { |cmd, arg| send(cmd, db, arg) }
  end
end

Private Instance Methods

backup(db, path) click to toggle source

Saves db to another location.

# File lib/perfect_world/cli.rb, line 36
def backup(db, path)
  db.save(path, @config['owner'])
end
database(_ = nil, _ = nil) click to toggle source

Prints the path to the currently used database.

# File lib/perfect_world/cli.rb, line 41
def database(_ = nil, _ = nil)
  print_config('database')
end
delete(db, id) click to toggle source

Deletes a password.

# File lib/perfect_world/cli.rb, line 46
def delete(db, id)
  if db.delete(id)
    puts "Deleted your #{id} password."
  else
    password_not_found(id)
  end
end
generate(db, id) click to toggle source

Generats a new password and sends it to stdout.

# File lib/perfect_world/cli.rb, line 55
def generate(db, id)
  len = @config.fetch('length')
  pwd = @config['force'] ? db.generate!(id, len) : db.generate(id, len)
  print_or_copy_to_clipboard(id, pwd)
rescue Error => e
  raise Error, "Couldn't create password: #{e.message}"
end
get(db, id) click to toggle source

Fetches the password and sends it to stdout.

# File lib/perfect_world/cli.rb, line 64
def get(db, id)
  print_or_copy_to_clipboard(id, db.fetch(id))
rescue KeyError
  password_not_found(id)
end
length(_ = nil, _ = nil) click to toggle source

Prints the currently used password length.

# File lib/perfect_world/cli.rb, line 71
def length(_ = nil, _ = nil)
  print_config('length')
end
list(db, _ = nil) click to toggle source

Lists all passwords.

# File lib/perfect_world/cli.rb, line 76
def list(db, _ = nil)
  db.each { |id, password| puts "#{password}   #{id}" }
end
owner(_ = nil, _ = nil) click to toggle source

Prints the current key owner.

# File lib/perfect_world/cli.rb, line 81
def owner(_ = nil, _ = nil)
  puts Storage.find_key(@config['owner']).email
end
password_not_found(id) click to toggle source

Raises an error with a proper message.

# File lib/perfect_world/cli.rb, line 86
def password_not_found(id)
  raise Error, "Couldn't find your #{id} password."
end
print_config(key) click to toggle source

Prints a config entry.

print_or_copy_to_clipboard(id, password) click to toggle source

Prints a password or copies it to the clipboard.