Class: KBSecret::CLI
- Inherits:
-
Object
- Object
- KBSecret::CLI
- Defined in:
- lib/kbsecret/cli.rb
Overview
An encapsulation of useful methods for kbsecret's CLI. Most methods in this class assume that they are being called from the context of a command-line utility.
Constant Summary collapse
- TYPE_ALIASES =
Abbreviations for record types (e.g.,
envforenvironment). Hash.new { |_, k| k }.update(Abbrev.abbrev(Record.record_types)).freeze
Instance Attribute Summary collapse
-
#args ⇒ Dreck::Result?
readonly
The result of trailing argument parsing, if requested via #dreck.
-
#opts ⇒ Slop::Result?
readonly
The result of option parsing, if requested via #slop.
-
#session ⇒ Session?
readonly
The session associated with the command, if requested via #ensure_session!.
Class Method Summary collapse
-
.create {|CLI| ... } ⇒ CLI
Encapsulate both the options and trailing arguments passed to a
kbsecretcommand. -
.die(msg) ⇒ void
Print an error message and terminate.
-
.ifs ⇒ String
Finds a reasonable default field separator by checking the environment first and then falling back to ":".
Instance Method Summary collapse
-
#bye(msg) ⇒ void
Print an informational message via #info and exit successfully.
-
#die(msg) ⇒ void
Print an error message and terminate.
-
#dreck(errors: true, &block) ⇒ Object
Parse trailing arguments for a kbsecret utility, using the elements remaining after options have been removed and interpreted via #slop.
-
#ensure_generator!(where = :option) ⇒ void
Ensure that a generator profile passed in as an option or argument already exists (i.e., is already configured).
-
#ensure_session!(where = :option) ⇒ void
Ensure that a session passed in as an option or argument already exists (i.e., is already configured).
-
#ensure_type!(where = :option) ⇒ void
Ensure that a record type passed in as an option or argument is resolvable to a record class.
-
#guard ⇒ Object
"Guard" a block by propagating any exceptions as fatal (unrecoverable) errors.
-
#info(msg) ⇒ void
Print an informational message if verbose output has been enabled.
-
#initialize ⇒ CLI
constructor
deprecated
private
Deprecated.
see CLI.create
-
#slop(cmds: [], errors: true) ⇒ Slop::Result
Parse options for a kbsecret utility, adding some default options for introspection, verbosity, and help output.
-
#warn(msg) ⇒ void
Print a warning message unless warnings have been suppressed.
Constructor Details
#initialize ⇒ CLI
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
see create
Returns a new instance of CLI.
56 57 58 59 |
# File 'lib/kbsecret/cli.rb', line 56 def initialize @argv = ARGV.dup guard { yield self } end |
Instance Attribute Details
#args ⇒ Dreck::Result? (readonly)
Returns the result of trailing argument parsing, if requested via #dreck.
25 26 27 |
# File 'lib/kbsecret/cli.rb', line 25 def args @args end |
#opts ⇒ Slop::Result? (readonly)
Returns the result of option parsing, if requested via #slop.
21 22 23 |
# File 'lib/kbsecret/cli.rb', line 21 def opts @opts end |
#session ⇒ Session? (readonly)
Returns the session associated with the command, if requested via #ensure_session!.
29 30 31 |
# File 'lib/kbsecret/cli.rb', line 29 def session @session end |
Class Method Details
.create {|CLI| ... } ⇒ CLI
Encapsulate both the options and trailing arguments passed to a kbsecret command.
50 51 52 |
# File 'lib/kbsecret/cli.rb', line 50 def self.create(&block) CLI.new(&block) end |
.die(msg) ⇒ void
This method does not return!
This method returns an undefined value.
Print an error message and terminate.
195 196 197 198 |
# File 'lib/kbsecret/cli.rb', line 195 def die(msg) pretty = "#{RED["Fatal"]}: #{msg}" abort pretty end |
.ifs ⇒ String
Finds a reasonable default field separator by checking the environment first and then falling back to ":".
203 204 205 |
# File 'lib/kbsecret/cli.rb', line 203 def ifs ENV["IFS"] || ":" end |
Instance Method Details
#bye(msg) ⇒ void
This method does not return!
This method returns an undefined value.
Print an informational message via #info and exit successfully.
168 169 170 171 |
# File 'lib/kbsecret/cli.rb', line 168 def bye(msg) info msg exit end |
#die(msg) ⇒ void
This method does not return!
This method returns an undefined value.
Print an error message and terminate.
185 186 187 188 |
# File 'lib/kbsecret/cli.rb', line 185 def die(msg) pretty = "#{RED["Fatal"]}: #{msg}" abort pretty end |
#dreck(errors: true, &block) ⇒ Object
96 97 98 99 100 |
# File 'lib/kbsecret/cli.rb', line 96 def dreck(errors: true, &block) @args = Dreck.parse @argv, strict: errors do instance_eval(&block) end end |
#ensure_generator!(where = :option) ⇒ void
139 140 141 142 |
# File 'lib/kbsecret/cli.rb', line 139 def ensure_generator!(where = :option) gen = where == :option ? @opts[:generator] : @args[:generator] Config.generator gen end |
#ensure_session!(where = :option) ⇒ void
111 112 113 114 |
# File 'lib/kbsecret/cli.rb', line 111 def ensure_session!(where = :option) label = where == :option ? @opts[:session] : @args[:session] @session = Session.new label: label end |
#ensure_type!(where = :option) ⇒ void
125 126 127 128 |
# File 'lib/kbsecret/cli.rb', line 125 def ensure_type!(where = :option) type = TYPE_ALIASES[where == :option ? @opts[:type] : @args[:type]] Record.class_for type end |
#guard ⇒ Object
This should be used to guard chunks of code that are likely to raise exceptions. The amount of code guarded should be minimized.
"Guard" a block by propagating any exceptions as fatal (unrecoverable) errors.
149 150 151 152 153 154 |
# File 'lib/kbsecret/cli.rb', line 149 def guard yield rescue => e STDERR.puts e.backtrace if @opts&.debug? die "#{e.to_s.capitalize}." end |
#info(msg) ⇒ void
This method returns an undefined value.
Print an informational message if verbose output has been enabled.
159 160 161 162 |
# File 'lib/kbsecret/cli.rb', line 159 def info(msg) return unless @opts.verbose? STDERR.puts "#{GREEN["Info"]}: #{msg}" end |
#slop(cmds: [], errors: true) ⇒ Slop::Result
This should be called within the block passed to #initialize.
Parse options for a kbsecret utility, adding some default options for introspection, verbosity, and help output.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/kbsecret/cli.rb', line 67 def slop(cmds: [], errors: true) @opts = Slop.parse @argv, suppress_errors: !errors do |o| o.separator "Options:" yield o o.bool "-V", "--verbose", "produce more verbose output" o.bool "-w", "--no-warn", "suppress warning messages" o.bool "--debug", "produce full backtraces on errors" o.on "-h", "--help", "show this help message" do puts o.to_s prefix: " " exit end o.on "--introspect-flags", "dump recognized flags and subcommands" do comp = o..flat_map(&:flags) + cmds puts comp.join "\n" exit end end @argv = @opts.args end |
#warn(msg) ⇒ void
This method returns an undefined value.
Print a warning message unless warnings have been suppressed.
176 177 178 179 |
# File 'lib/kbsecret/cli.rb', line 176 def warn(msg) return if @opts.no_warn? STDERR.puts "#{YELLOW["Warning"]}: #{msg}" end |