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.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sym/application.rb', line 24

def initialize(opts)
  self.opts_original = opts
  self.opts          = opts.is_a?(Hash) ? opts : opts.to_hash

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

  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.



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

def action
  @action
end

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

#input_handlerObject

Returns the value of attribute input_handler.



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

def input_handler
  @input_handler
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#key_handlerObject

Returns the value of attribute key_handler.



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

def key_handler
  @key_handler
end

#key_sourceObject

Returns the value of attribute key_source.



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

def key_source
  @key_source
end

#optsObject

Returns the value of attribute opts.



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

def opts
  @opts
end

#opts_originalObject

Returns the value of attribute opts_original.



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

def opts_original
  @opts_original
end

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

#password_cacheObject

Returns the value of attribute password_cache.



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

def password_cache
  @password_cache
end

#provided_optionsObject

Returns the value of attribute provided_options.



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

def provided_options
  @provided_options
end

#resultObject

Returns the value of attribute result.



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

def result
  @result
end

Instance Method Details

#commandObject



76
77
78
79
80
# File 'lib/sym/application.rb', line 76

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

#editorObject



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

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

#executeObject



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

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

rescue StandardError => e
  { exception: e }
end

#execute!Object



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

def execute!
  initialize_key_source
  unless command
    raise Sym::Errors::InsufficientOptionsError,
          " Can not determine what to do
    from the options: \ n " +
            " #{self.provided_options.inspect.green.bold}\n" +
            "and flags #{self.provided_flags.to_s.green.bold}"
  end
  log :info, "command located is #{command.class.name.blue.bold}"
  self.result = command.execute.tap do |result|
    log :info, "result is  #{result.nil? ? 'nil' : result[0..10].to_s.blue.bold }..." if opts[:trace]
  end
end

#log(*args) ⇒ Object



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

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

#provided_flagsObject



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

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

#provided_safe_optionsObject



96
97
98
99
100
101
102
# File 'lib/sym/application.rb', line 96

def provided_safe_options
  provided_options.map do |k, v|
    k == :key && [44, 45].include?(v.size) ?
      [k, '[reducted]'] :
      [k, v]
  end.to_h
end

#provided_value_optionsObject



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

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