Sha256: 5305e30a8e4501606633b32752bb528da16300bbc8252eed2201ae3b0a29d8e0

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

require 'setec_astronomy'
require 'setec_astronomy/prompt'

require 'thor'
require 'clipboard'

module SetecAstronomy
  class CLI < Thor
    desc "search PATTERN", "searches the database for entries matching the pattern"
    method_option :file, :type => :string, :required => true, :aliases => '-f'
    method_option :gui, :type => :boolean, :aliases => '-g'
    def search(pattern)
      keepass = database(options[:file], options[:gui])
      keepass.search(pattern).each do |match|
        puts "#{match.title} - #{match.notes}"
      end
    end

    desc "copy ENTRY", "copies the password for the given entry to the system clipboard"
    method_option :file, :type => :string, :required => true, :aliases => '-f'
    method_option :gui, :type => :boolean, :aliases => '-g'
    def copy(title)
      keepass = database(options[:file], options[:gui])
      entry = keepass.entry(title)
      resign("#{title} not found") if entry.nil?
      Clipboard.copy entry.password
    end

    no_tasks do
      def database(file, gui=false)
        db = KeePass::Database.open(file)
        password = gui ? Prompt.ask_password_gui : Prompt.ask_password_console
        resign("Unable to unlock database... exiting") unless db.unlock password
        db
      end

      def resign(error)
        puts error
        exit 1
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
setec_astronomy-0.2.1 lib/setec_astronomy/cli.rb
setec_astronomy-0.2.0 lib/setec_astronomy/cli.rb
setec_astronomy-0.1.0 lib/setec_astronomy/cli.rb