Class: Sym::Application

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Application

Returns a new instance of Application.



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

def initialize(opts)
  self.opts      = opts
  self.opts_hash = opts.respond_to?(:to_hash) ? opts.to_hash : opts
  self.args      = ::Sym::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.



9
10
11
# File 'lib/sym/application.rb', line 9

def action
  @action
end

#argsObject

Returns the value of attribute args.



9
10
11
# File 'lib/sym/application.rb', line 9

def args
  @args
end

#input_handlerObject

Returns the value of attribute input_handler.



9
10
11
# File 'lib/sym/application.rb', line 9

def input_handler
  @input_handler
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/sym/application.rb', line 9

def key
  @key
end

#key_handlerObject

Returns the value of attribute key_handler.



9
10
11
# File 'lib/sym/application.rb', line 9

def key_handler
  @key_handler
end

#optsObject

Returns the value of attribute opts.



9
10
11
# File 'lib/sym/application.rb', line 9

def opts
  @opts
end

#opts_hashObject

Returns the value of attribute opts_hash.



9
10
11
# File 'lib/sym/application.rb', line 9

def opts_hash
  @opts_hash
end

#password_cacheObject

Returns the value of attribute password_cache.



9
10
11
# File 'lib/sym/application.rb', line 9

def password_cache
  @password_cache
end

#resultObject

Returns the value of attribute result.



9
10
11
# File 'lib/sym/application.rb', line 9

def result
  @result
end

Instance Method Details

#commandObject



81
82
83
84
85
# File 'lib/sym/application.rb', line 81

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

#editorObject



87
88
89
# File 'lib/sym/application.rb', line 87

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

#editors_to_tryObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sym/application.rb', line 91

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

#executeObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sym/application.rb', line 58

def execute
  execute!

rescue ::OpenSSL::Cipher::CipherError => e
  { reason:    'Invalid key provided',
    exception: e }

rescue Sym::Errors::Error => e
  { reason:    e.class.name.gsub(/.*::/, '').underscore.humanize.downcase,
    exception: e }

rescue TypeError => e
  if e.message =~ /marshal/
    { reason: 'Corrupt source data or invalid/corrupt key provided',
      exception: e }
  else
    { exception: e }
  end

rescue StandardError => e
  { exception: e }
end

#execute!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sym/application.rb', line 38

def execute!
  if !args.generate_key? && (args.require_key? || args.specify_key?)
    log :debug, 'operation requires a key...'
    self.key = Sym::App::PrivateKey::Handler.new(opts, input_handler, password_cache).key
    unless self.key
      log :error, 'Unable to determine the key, which appears to be required'
      raise Sym::Errors::NoPrivateKeyFound, 'Private key is required'
    end
  end
  log :info, "detected command [#{command.class.name}]"
  unless command
    raise Sym::Errors::InsufficientOptionsError, '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



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

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

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



106
107
108
# File 'lib/sym/application.rb', line 106

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

#initialize_key_handlerObject



110
111
112
# File 'lib/sym/application.rb', line 110

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

#initialize_password_cacheObject



114
115
116
117
118
119
120
121
122
# File 'lib/sym/application.rb', line 114

def initialize_password_cache
  args            = {}
  args[:timeout]  = opts[:cache_for].to_i if opts[:cache_for]
  args[:enabled]  = opts[:cache_password]
  args[:verbose]  = opts[:verbose]
  args[:provider] = opts[:cache_provider] if opts[:cache_provider]

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

#log(*args) ⇒ Object



54
55
56
# File 'lib/sym/application.rb', line 54

def log(*args)
  Sym::App.log(*args, **opts)
end