Class: Secrets::App::CLI

Inherits:
Object
  • Object
show all
Includes:
Secrets
Defined in:
lib/secrets/app/cli.rb

Constant Summary

Constants included from Secrets

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Secrets

included

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/secrets/app/cli.rb', line 29

def initialize(argv)
  begin
    self.opts = parse(argv.dup)
  rescue StandardError => e
    error exception: e
    return
  end
  configure_color(argv)
  define_output
  self.action = { opts[:encrypt] => :encr, opts[:decrypt] => :decr }[true]
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



21
22
23
# File 'lib/secrets/app/cli.rb', line 21

def action
  @action
end

#keyObject

Returns the value of attribute key.



21
22
23
# File 'lib/secrets/app/cli.rb', line 21

def key
  @key
end

#optsObject

Returns the value of attribute opts.



21
22
23
# File 'lib/secrets/app/cli.rb', line 21

def opts
  @opts
end

#output_procObject

Returns the value of attribute output_proc.



21
22
23
# File 'lib/secrets/app/cli.rb', line 21

def output_proc
  @output_proc
end

#passwordObject

Returns the value of attribute password.



21
22
23
# File 'lib/secrets/app/cli.rb', line 21

def password
  @password
end

Returns the value of attribute print_proc.



21
22
23
# File 'lib/secrets/app/cli.rb', line 21

def print_proc
  @print_proc
end

#write_procObject

Returns the value of attribute write_proc.



21
22
23
# File 'lib/secrets/app/cli.rb', line 21

def write_proc
  @write_proc
end

Instance Method Details

#commandObject



41
42
43
44
# File 'lib/secrets/app/cli.rb', line 41

def command
  @command_class ||= Secrets::App::Commands.find_command_class(opts)
  @command       ||= @command_class.new(self) if @command_class
end

#editorObject



86
87
88
# File 'lib/secrets/app/cli.rb', line 86

def editor
  ENV['EDITOR'] || '/bin/vi'
end

#error(hash) ⇒ Object



82
83
84
# File 'lib/secrets/app/cli.rb', line 82

def error(hash)
  Secrets::App.error(hash.merge(config: (opts ? opts.to_hash : {})))
end

#runObject



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
# File 'lib/secrets/app/cli.rb', line 46

def run
  return Secrets::App.exit_code if Secrets::App.exit_code != 0

  define_private_key
  decrypt_private_key if should_decrypt_private_key?
  verify_private_key_encoding if key

  if command
    return self.output_proc.call(command.run)
  else
    # command was not found. Reset output to printing, and return an error.
    self.output_proc = print_proc
    command_not_found_error!
  end

rescue ::OpenSSL::Cipher::CipherError => e
  error type:      'Cipher Error',
        details:   e.message,
        reason:    'Perhaps either the secret is invalid, or encrypted data is corrupt.',
        exception: e

rescue Secrets::Errors::InvalidEncodingPrivateKey => e
  error type:      'Private Key Error',
        details:   'Private key does not appear to be properly encoded. ',
        reason:    (opts[:password] ? nil : 'Perhaps the key is password-protected?'),
        exception: e

rescue Secrets::Errors::Error => e
  error type:      'Error',
        details:   e.message,
        exception: e

rescue StandardError => e
  error exception: e
end