Class: Sym::App::CLI

Inherits:
Object show all
Includes:
CLISlop
Defined in:
lib/sym/app/cli.rb

Overview

This is the main interface class for the CLI application. It is responsible for parsing user’s input, providing help, examples, coordination of various sub-systems (such as PrivateKey detection), etc.

Besides holding the majority of the application state, it contains two primary public methods: #new and #run.

The constructor is responsible for parsing the flags and determining the the application is about to do. It sets up input/output, but doesn’t really execute any encryption or decryption. This happens in the #run method called immediately after #new.

{Shh{Shh::App{Shh::App::CLI} module effectively performs the translation of the opts object (of type Slop::Result) and interpretation of users intentions. It holds on to opts for the duration of the program.

Responsibility Delegated

The responsibility of determining the private key from various options provided is performed by the PrivateKey::Handler instance. See there for more details.

Subsequently, #run method handles the finding of the appropriate Sym::App::Commands::BaseCommand subclass to respond to user’s request. Command registry, sorting, command dependencies, and finding them is done by the Coommands module.

User input is handled by the Input::Handler instance, while the output is provided by the procs in the Output classes.

Finally, the Mac OS-X -specific usage of the KeyChain, is encapsulated in a cross-platform way inside the Keychain module.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CLISlop

#key_spec, #parse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sym/app/cli.rb', line 61

def initialize(argv)
  begin

    # Re-map any legacy options to the new options
    self.opts = parse(argv)
    if opts[:sym_args]
      append_sym_args(argv)
      self.opts = parse(argv)
    end

    # Disable coloring if requested, or if piping STDOUT
    if opts[:no_color] || !STDOUT.tty?
      Colored2.disable! # reparse options without the colors to create new help msg
      self.opts = parse(argv)
    end

  rescue StandardError => e
    log :error, "#{e.message}" if opts
    error exception: e
    return
  end

  self.application = ::Sym::Application.new(opts)
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



59
60
61
# File 'lib/sym/app/cli.rb', line 59

def application
  @application
end

#optsObject

Returns the value of attribute opts.



59
60
61
# File 'lib/sym/app/cli.rb', line 59

def opts
  @opts
end

#outputsObject

Returns the value of attribute outputs.



59
60
61
# File 'lib/sym/app/cli.rb', line 59

def outputs
  @outputs
end

Instance Method Details

#append_sym_args(argv) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/sym/app/cli.rb', line 86

def append_sym_args(argv)
  if env_args = sym_args
    argv << env_args.split(' ')
    argv.flatten!
    argv.compact!
  end
end

#commandObject



108
109
110
# File 'lib/sym/app/cli.rb', line 108

def command
  @command ||= self.application&.command
end

#executeObject



98
99
100
101
102
103
104
105
106
# File 'lib/sym/app/cli.rb', line 98

def execute
  return Sym::App.exit_code if Sym::App.exit_code != 0
  result = application.execute
  if result.is_a?(Hash)
    self.output_proc ::Sym::App::Args.new({}).output_class
    error(result)
  end
  Sym::App.exit_code
end

#opts_presentObject



117
118
119
120
121
# File 'lib/sym/app/cli.rb', line 117

def opts_present
  o = opts.to_hash
  o.keys.map { |k| opts[k] ? nil : k }.compact.each { |k| o.delete(k) }
  o
end

#output_proc(proc = nil) ⇒ Object



112
113
114
115
# File 'lib/sym/app/cli.rb', line 112

def output_proc(proc = nil)
  self.application&.output = proc if proc
  self.application&.output
end

#sym_argsObject



94
95
96
# File 'lib/sym/app/cli.rb', line 94

def sym_args
  ENV[Sym::Constants::ENV_ARGS_VARIABLE_NAME]
end