Class: Shhh::App::CLI

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/shhh/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 Shhh::App::Commands::Command 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

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



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

def initialize(argv)
  begin
    argv_copy = argv.dup
    dict      = false
    if argv_copy.include?('--dictionary')
      dict = true
      argv_copy.delete('--dictionary')
    end
    self.opts = parse(argv_copy)
    if dict
      options = opts.parser.unused_options + opts.parser.used_options
      puts options.map { |o| o.to_s.gsub(/.*(--[\w-]+).*/, '\1') }.sort.join(' ')
      exit 0
    end
  rescue StandardError => e
    error exception: e
    return
  end

  configure_color(argv)

  self.application = ::Shhh::Application.new(opts)
  select_output_stream
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



60
61
62
# File 'lib/shhh/app/cli.rb', line 60

def application
  @application
end

#optsObject

Returns the value of attribute opts.



60
61
62
# File 'lib/shhh/app/cli.rb', line 60

def opts
  @opts
end

#output_procObject

Returns the value of attribute output_proc.



60
61
62
# File 'lib/shhh/app/cli.rb', line 60

def output_proc
  @output_proc
end

#outputsObject

Returns the value of attribute outputs.



60
61
62
# File 'lib/shhh/app/cli.rb', line 60

def outputs
  @outputs
end

Instance Method Details

#executeObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/shhh/app/cli.rb', line 87

def execute
  return Shhh::App.exit_code if Shhh::App.exit_code != 0

  result = application.execute
  if result.is_a?(Hash)
    self.output_proc = ::Shhh::App::Args.new({}).output_class
    error(result)
  else
    self.output_proc.call(result)
  end
end