Class: Shhh::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/shhh/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Application

Returns a new instance of Application.


18
19
20
21
22
23
24
25
26
27
# File 'lib/shhh/application.rb', line 18

def initialize(opts)
  self.opts      = opts
  self.opts_hash = opts.respond_to?(:to_hash) ? opts.to_hash : opts
  self.args      = ::Shhh::App::Args.new(opts_hash)

  initialize_password_cache
  initialize_input_handler
  initialize_key_handler
  initialize_action
end

Instance Attribute Details

#actionObject

Returns the value of attribute action


8
9
10
# File 'lib/shhh/application.rb', line 8

def action
  @action
end

#argsObject

Returns the value of attribute args


8
9
10
# File 'lib/shhh/application.rb', line 8

def args
  @args
end

#input_handlerObject

Returns the value of attribute input_handler


8
9
10
# File 'lib/shhh/application.rb', line 8

def input_handler
  @input_handler
end

#keyObject

Returns the value of attribute key


8
9
10
# File 'lib/shhh/application.rb', line 8

def key
  @key
end

#key_handlerObject

Returns the value of attribute key_handler


8
9
10
# File 'lib/shhh/application.rb', line 8

def key_handler
  @key_handler
end

#optsObject

Returns the value of attribute opts


8
9
10
# File 'lib/shhh/application.rb', line 8

def opts
  @opts
end

#opts_hashObject

Returns the value of attribute opts_hash


8
9
10
# File 'lib/shhh/application.rb', line 8

def opts_hash
  @opts_hash
end

#password_cacheObject

Returns the value of attribute password_cache


8
9
10
# File 'lib/shhh/application.rb', line 8

def password_cache
  @password_cache
end

#resultObject

Returns the value of attribute result


8
9
10
# File 'lib/shhh/application.rb', line 8

def result
  @result
end

Instance Method Details

#commandObject


68
69
70
71
72
# File 'lib/shhh/application.rb', line 68

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

#editorObject


74
75
76
# File 'lib/shhh/application.rb', line 74

def editor
  editors_to_try.find { |editor| File.exist?(editor) }
end

#editors_to_tryObject


78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/shhh/application.rb', line 78

def editors_to_try
  [
    ENV['EDITOR'],
    '/usr/bin/vim',
    '/usr/local/bin/vim',
    '/bin/vim',
    '/sbin/vim',
    '/usr/sbin/vim',
    '/usr/bin/vi',
    '/usr/local/bin/vi',
    '/bin/vi',
    '/sbin/vi'
  ]
end

#error(hash) ⇒ Object


93
94
95
# File 'lib/shhh/application.rb', line 93

def error(hash)
  hash
end

#executeObject


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shhh/application.rb', line 51

def execute
  execute!

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 Shhh::Errors::Error => e
  error type:    e.class.name.split(/::/)[-1],
        details: e.message

rescue StandardError => e
  error exception: e
end

#execute!Object


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/shhh/application.rb', line 37

def execute!
  if !args.generate_key? &&
    (args.require_key? || args.specify_key?)
    self.key = Shhh::App::PrivateKey::Handler.new(opts, input_handler, password_cache).key
    raise Shhh::Errors::NoPrivateKeyFound.new('Private key is required') unless self.key
  end

  unless command
    raise Shhh::Errors::InsufficientOptionsError.new(
      'Can not determine what to do from the options ' + opts_hash.keys.reject { |k| !opts[k] }.to_s)
  end
  self.result = command.execute
end

#initialize_actionObject


29
30
31
32
33
34
35
# File 'lib/shhh/application.rb', line 29

def initialize_action
  self.action = if opts[:encrypt] then
                  :encr
                elsif opts[:decrypt]
                  :decr
                end
end

#initialize_input_handler(handler = ::Shhh::App::Input::Handler.new) ⇒ Object


97
98
99
# File 'lib/shhh/application.rb', line 97

def initialize_input_handler(handler = ::Shhh::App::Input::Handler.new)
  self.input_handler = handler
end

#initialize_key_handlerObject


101
102
103
# File 'lib/shhh/application.rb', line 101

def initialize_key_handler
  self.key_handler = ::Shhh::App::PrivateKey::Handler.new(self.opts, input_handler, password_cache)
end

#initialize_password_cacheObject


105
106
107
108
109
110
111
112
# File 'lib/shhh/application.rb', line 105

def initialize_password_cache
  args            = {}
  args[:provider] = Coin
  args[:timeout]  = opts[:password_timeout].to_i if opts[:password_timeout]
  args[:enabled]  = false if opts[:no_password_cache]

  self.password_cache = Shhh::App::Password::Cache.instance.configure(args)
end