Class: Sym::Application

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

Overview

Main Application controller class for Sym.

Accepts a hash with CLI options set (as symbols), for example

Example

app = Sym::Application.new( encrypt: true, file: '/tmp/secrets.yml', output: '/tmp/secrets.yml.enc')
result = app.execute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = nil) ⇒ Application

Returns a new instance of Application.



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

def initialize(opts, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = nil)
  self.stdin  = stdin
  self.stdout = stdout
  self.stderr = stderr
  self.kernel = kernel

  self.opts_slop = opts.clone
  self.opts      = opts.is_a?(Hash) ? opts : opts.to_hash

  process_negated_option(opts[:negate]) if opts[:negate]
  process_edit_option

  self.args = ::Sym::App::Args.new(self.provided_options)

  initialize_output_stream
  initialize_action
  initialize_data_source
  initialize_password_cache
  initialize_input_handler
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



21
22
23
# File 'lib/sym/application.rb', line 21

def action
  @action
end

#argsObject

Returns the value of attribute args.



21
22
23
# File 'lib/sym/application.rb', line 21

def args
  @args
end

#input_handlerObject

Returns the value of attribute input_handler.



21
22
23
# File 'lib/sym/application.rb', line 21

def input_handler
  @input_handler
end

#kernelObject

Returns the value of attribute kernel.



21
22
23
# File 'lib/sym/application.rb', line 21

def kernel
  @kernel
end

#keyObject

Returns the value of attribute key.



21
22
23
# File 'lib/sym/application.rb', line 21

def key
  @key
end

#key_handlerObject

Returns the value of attribute key_handler.



21
22
23
# File 'lib/sym/application.rb', line 21

def key_handler
  @key_handler
end

#key_sourceObject

Returns the value of attribute key_source.



21
22
23
# File 'lib/sym/application.rb', line 21

def key_source
  @key_source
end

#optsObject

Returns the value of attribute opts.



21
22
23
# File 'lib/sym/application.rb', line 21

def opts
  @opts
end

#opts_slopObject

Returns the value of attribute opts_slop.



21
22
23
# File 'lib/sym/application.rb', line 21

def opts_slop
  @opts_slop
end

#outputObject

Returns the value of attribute output.



21
22
23
# File 'lib/sym/application.rb', line 21

def output
  @output
end

#password_cacheObject

Returns the value of attribute password_cache.



21
22
23
# File 'lib/sym/application.rb', line 21

def password_cache
  @password_cache
end

#resultObject

Returns the value of attribute result.



21
22
23
# File 'lib/sym/application.rb', line 21

def result
  @result
end

#stderrObject

Returns the value of attribute stderr.



21
22
23
# File 'lib/sym/application.rb', line 21

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



21
22
23
# File 'lib/sym/application.rb', line 21

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



21
22
23
# File 'lib/sym/application.rb', line 21

def stdout
  @stdout
end

Instance Method Details

#commandObject



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

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

#editorObject



112
113
114
# File 'lib/sym/application.rb', line 112

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

#executeObject

Main action method — it looksup the command, and executes it, translating various exception conditions into meaningful error messages.



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

def execute
  process_output(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/m
    { reason:    'Corrupt source data or invalid/corrupt key provided',
      exception: e }
  else
    { exception: e }
  end

rescue StandardError => e
  { exception: e }
end

#process_output(result) ⇒ Object



116
117
118
119
# File 'lib/sym/application.rb', line 116

def process_output(result)
  self.output.call(result) unless result.is_a?(Hash)
  result
end

#provided_flagsObject



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

def provided_flags
  provided_flags = provided_options
  provided_flags.delete_if { |k, v| ![false, true].include?(v) }
  provided_flags.keys
end

#provided_options(**opts) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sym/application.rb', line 98

def provided_options(**opts)
  provided_opts = self.opts.clone
  provided_opts.delete_if { |k, v| !v }
  if opts[:safe]
    provided_options.map do |k, v|
      k == :key && [44, 45].include?(v.size) ?
        [k, '[reducted]'] :
        [k, v]
    end.to_h
  else
    provided_opts
  end
end

#provided_value_optionsObject



92
93
94
95
96
# File 'lib/sym/application.rb', line 92

def provided_value_options
  provided = provided_options(safe: true)
  provided.delete_if { |k, v| [false, true].include?(v) }
  provided
end