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

def self.main(args)
  # parse args in cli and denote using cli
  settings = parse(args)

  # validate args
  if i[encrypt decrypt].include?(settings[:action])
    args.empty? ? (raise 'rapid-vaults: no file specified; try using --help') : settings[:file] = args.first
  end

  raise 'input password cannot be empty' if settings.key?(:pw) && settings[:pw].empty?

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

.parse(args) ⇒ Object

parse cli options



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rapid-vaults/cli.rb', line 23

def self.parse(args)
  require 'optparse'

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

  # init settings with cli setting
  settings = { ui: :cli }

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

    # base options
    opts.on('--version', 'Display the current version.') do
      require 'rubygems'

      puts Gem::Specification.load("#{File.dirname(__FILE__)}/../../rapid-vaults.gemspec").version
      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: keys only).') { settings[:action] = :generate }
    opts.on('-e', '--encrypt', 'Encrypt a file using a key and nonce and generate a tag (GPG: key and pw only).') { settings[:action] = :encrypt }
    opts.on('-d', '--decrypt', 'Decrypt a file using a key, nonce, and tag (GPG: key and pw only).') { settings[:action] = :decrypt }

    # key, nonce, password, and tag
    opts.on('-k', '--key key', String, 'Key file to be used for encryption or decryption. (GPG: use GNUPGHOME)') { |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).') do |arg|
      raise "Password file #{arg} is not an existing readable file!" unless File.readable?(arg)
      settings[:pw] = File.read(arg)
    end

    # bindings
    opts.on('-b', '--binding binding', String, 'Output files to support bindings for other software languages.') do |arg|
      settings[:action] = :binding
      settings[:binding] = arg.to_sym
    end

    # other
    opts.on('--gpgparams params.txt', String, 'GPG Key params input file used during generation of keys.') do |arg|
      raise "GPG Parameters file #{arg} is not an existing readable file!" unless File.readable?(arg)
      settings[:gpgparams] = File.read(arg)
    end
    opts.on('-o --outdir', String, 'Optional output directory for generated files (default: pwd). (GPG: optional)') do |arg|
      raise "The output directory #{arg} does not exist or is not a directory!" unless File.directory?(arg)
      settings[:outdir] = arg
    end
  end

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