Class: RapidVaults::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rapid-vaults/cli.rb

Overview

provides a command line interface to interact with rapid vaults

Class Method Summary collapse

Class Method Details

.main(args) ⇒ Object

point of entry from executable



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rapid-vaults/cli.rb', line 6

def self.main(args)
  # parse args in cli and denote using cli
  settings = parse(args)
  unless settings[:action] == :generate
    args.empty? ? (raise 'rapid-vaults: no file specified; try using --help') : settings[:file] = args.first
  end

  # run RapidVaults with specified file
  RapidVaults.new.main(settings)
  0
end

.parse(args) ⇒ Object

parse cli options



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
64
# File 'lib/rapid-vaults/cli.rb', line 19

def self.parse(args)
  require 'optparse'

  # show help message if no args specified
  args = %w[-h] if args.empty?

  # init settings
  settings = {}
  # specify cli being used
  settings[:ui] = :cli
  # default to openssl algorithm
  settings[:algorithm] = :openssl

  opt_parser = OptionParser.new do |opts|
    # usage
    opts.banner = 'usage: rapid-vaults [options] file'

    # base options
    opts.on('--version', 'Display the current version.') do
      puts 'rapid-vaults 1.1.0'
      exit 0
    end

    # use gpg instead
    opts.on('--gpg', 'Use GNUPG/GPG instead of GNUTLS/OpenSSL for encryption/decryption.') { settings[:algorithm] = :gpgme }

    # generate, encrypt, or decrypt
    opts.on('-g', '--generate', 'Generate a key and nonce for encryption and decryption (GPG: n/a).') { settings[:action] = :generate }
    opts.on('-e', '--encrypt', 'Encrypt a file using a key and nonce and generate a tag (GPG: key only).') { settings[:action] = :encrypt }
    opts.on('-d', '--decrypt', 'Decrypt a file using a key, nonce, and tag (GPG: key only).') { settings[:action] = :decrypt }

    # key, nonce, password, and tag
    opts.on('-k', '--key key', String, 'Key file to be used for encryption or decryption.') { |arg| settings[:key] = arg }
    opts.on('-n', '--nonce nonce', String, 'Nonce file to be used for encryption or decryption (GPG: n/a).') { |arg| settings[:nonce] = arg }
    opts.on('-t', '--tag tag', String, 'Tag file to be used for decryption (GPG: n/a).') { |arg| settings[:tag] = arg }
    opts.on('-p', '--password password', String, '(optional) Password to be used for encryption or decryption (GPG: required).') { |arg| settings[:pw] = arg }
    opts.on('-f', '--file-password password.txt', String, '(optional) Text file containing a password to be used for encryption or decryption (GPG: required).') { |arg| settings[:pw] = File.read(arg) }

    # gpg params file
    opts.on('--gpgparams params.txt', String, 'GPG Key params input file used during generation of keys.') { |arg| settings[:gpgparams] = File.read(arg) }
  end

  # parse args and return settings
  opt_parser.parse!(args)
  settings
end