summaryrefslogtreecommitdiff
path: root/lib/trunk/cli.rb
blob: dc01bc0864c7c46bc5550a1035aa069a4f1e3708 (plain)
1
2
3
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Trunk
  class CLI < Thor
    desc "add NAME PASSWORD", "add a key and password"
    def add(name, password = $stdin.gets.strip)
      storage.store(name, password)
    end

    desc "show NAME", "print the password associated with a key"
    def show(name)
      print storage.fetch(name)
    rescue OpenSSL::PKey::RSAError => error
      say error.message, :red
    end

    desc "version", "print the version"
    def version
      say Trunk::VERSION
    end

    desc "setup", "create the database and private key file."
    method_option :algorithm, type: :string, default: 'AES-256-CBC'
    def setup
      FileUtils.mkdir_p(dir)
      exit(0) unless file_collision(private_key_path) if File.exist?(private_key_path)
      IO.write(private_key_path, OpenSSL::PKey::RSA.new(4096).export(options[:algorithm], passphrase))
      FileUtils.touch(database_path)
      [database_path, private_key_path].each { |x| FileUtils.chmod(0600, x) }
    end

    desc "import KDBX", "import a kdbx database"
    def import(file)
      Trunk::KDBX.new(file).each do |item|
        puts item.inspect
      end
    end

    private

    def storage
      @storage ||= Trunk::YAMLStorage.new(database_path, private_key)
    end

    def database_path
      File.join(dir, '.trunk.enc')
    end

    def private_key_path
      File.join(dir, '.trunk.key.enc')
    end

    def dir
      ENV['TRUNK_HOME'] || File.join(Dir.home, '.trunk')
    end

    def private_key
      OpenSSL::PKey::RSA.new(IO.read(private_key_path), passphrase)
    end

    def passphrase
      ENV['TRUNK_PASSPHRASE'] || ask("passphrase:", echo: false)
    end
  end
end