Class: KBSecret::CLI
- Inherits:
-
Object
- Object
- KBSecret::CLI
- Defined in:
- lib/kbsecret/cli.rb,
lib/kbsecret/cli/command.rb,
lib/kbsecret/cli/command/cp.rb,
lib/kbsecret/cli/command/rm.rb,
lib/kbsecret/cli/command/env.rb,
lib/kbsecret/cli/command/new.rb,
lib/kbsecret/cli/command/conf.rb,
lib/kbsecret/cli/command/help.rb,
lib/kbsecret/cli/command/list.rb,
lib/kbsecret/cli/command/pass.rb,
lib/kbsecret/cli/command/todo.rb,
lib/kbsecret/cli/command/login.rb,
lib/kbsecret/cli/command/types.rb,
lib/kbsecret/cli/command/session.rb,
lib/kbsecret/cli/command/version.rb,
lib/kbsecret/cli/command/abstract.rb,
lib/kbsecret/cli/command/commands.rb,
lib/kbsecret/cli/command/raw_edit.rb,
lib/kbsecret/cli/command/sessions.rb,
lib/kbsecret/cli/command/generator.rb,
lib/kbsecret/cli/command/generators.rb,
lib/kbsecret/cli/command/stash_edit.rb,
lib/kbsecret/cli/command/stash_file.rb,
lib/kbsecret/cli/command/dump_fields.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.
Defined Under Namespace
Modules: Command
Constant Summary collapse
- TYPE_ALIASES =
Abbreviations for record types (e.g.,
env
forenvironment
). 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(argv = ARGV) {|CLI| ... } ⇒ CLI
Encapsulate both the options and trailing arguments passed to a
kbsecret
command. -
.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 ":".
-
.installed?(util) ⇒ Boolean
Searches for an executable on the user's
$PATH
. -
.stderr ⇒ IO
The IO object corresponding to the current standard error.
-
.stdin ⇒ IO
The IO object corresponding to the current standard input.
-
.stdout ⇒ IO
The IO object corresponding to the current standard output.
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
command, 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.
-
#initialize(argv = ARGV) ⇒ CLI
constructor
deprecated
private
Deprecated.
see CLI.create
-
#prompt(question, echo: true) ⇒ String
Prompt the user for some input.
-
#slop(cmds: [], errors: true) ⇒ Slop::Result
Parse options for a
kbsecret
command, adding some default options for introspection, verbosity, and help output. -
#verbose(msg) ⇒ void
Print an information message, but only if verbose output has been enabled.
-
#warn(msg) ⇒ void
Print a warning message unless warnings have been suppressed.
Constructor Details
#initialize(argv = ARGV) ⇒ 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.
59 60 61 62 |
# File 'lib/kbsecret/cli.rb', line 59 def initialize(argv = ARGV) @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.
28 29 30 |
# File 'lib/kbsecret/cli.rb', line 28 def args @args end |
#opts ⇒ Slop::Result? (readonly)
Returns the result of option parsing, if requested via #slop.
24 25 26 |
# File 'lib/kbsecret/cli.rb', line 24 def opts @opts end |
#session ⇒ Session? (readonly)
Returns the session associated with the command, if requested via #ensure_session!.
32 33 34 |
# File 'lib/kbsecret/cli.rb', line 32 def session @session end |
Class Method Details
.create(argv = ARGV) {|CLI| ... } ⇒ CLI
Encapsulate both the options and trailing arguments passed to a kbsecret
command.
53 54 55 |
# File 'lib/kbsecret/cli.rb', line 53 def self.create(argv = ARGV, &block) CLI.new(argv, &block) end |
.die(msg) ⇒ void
This method does not return!
This method returns an undefined value.
Print an error message and terminate.
219 220 221 222 |
# File 'lib/kbsecret/cli.rb', line 219 def self.die(msg) fatal = ENV["NO_COLOR"] ? "Fatal" : RED["Fatal"] abort "#{fatal}: #{msg}" end |
.ifs ⇒ String
Finds a reasonable default field separator by checking the environment first and then falling back to ":".
227 228 229 |
# File 'lib/kbsecret/cli.rb', line 227 def self.ifs ENV["IFS"] || ":" end |
.installed?(util) ⇒ Boolean
Searches for an executable on the user's $PATH
.
255 256 257 258 259 |
# File 'lib/kbsecret/cli.rb', line 255 def self.installed?(util) ENV["PATH"].split(File::PATH_SEPARATOR).any? do |path| File.executable?(File.join(path, util)) end end |
.stderr ⇒ IO
Internal kbsecret
commands should use this, and not STDERR
.
Returns the IO object corresponding to the current standard error.
245 246 247 |
# File 'lib/kbsecret/cli.rb', line 245 def self.stderr $stderr end |
.stdin ⇒ IO
Internal kbsecret
commands should use this, and not STDIN
.
Returns the IO object corresponding to the current standard input.
233 234 235 |
# File 'lib/kbsecret/cli.rb', line 233 def self.stdin $stdin end |
.stdout ⇒ IO
Internal kbsecret
commands should use this, and not STDOUT
.
Returns the IO object corresponding to the current standard output.
239 240 241 |
# File 'lib/kbsecret/cli.rb', line 239 def self.stdout $stdout 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.
192 193 194 195 |
# File 'lib/kbsecret/cli.rb', line 192 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.
210 211 212 213 |
# File 'lib/kbsecret/cli.rb', line 210 def die(msg) fatal = ENV["NO_COLOR"] ? "Fatal" : RED["Fatal"] abort "#{fatal}: #{msg}" end |
#dreck(errors: true, &block) ⇒ Object
99 100 101 102 103 |
# File 'lib/kbsecret/cli.rb', line 99 def dreck(errors: true, &block) @args = Dreck.parse @argv, strict: errors do instance_eval(&block) end end |
#ensure_generator!(where = :option) ⇒ void
142 143 144 145 |
# File 'lib/kbsecret/cli.rb', line 142 def ensure_generator!(where = :option) gen = where == :option ? @opts[:generator] : @args[:generator] Config.generator gen end |
#ensure_session!(where = :option) ⇒ void
114 115 116 117 |
# File 'lib/kbsecret/cli.rb', line 114 def ensure_session!(where = :option) label = where == :option ? @opts[:session] : @args[:session] @session = Session[label] end |
#ensure_type!(where = :option) ⇒ void
128 129 130 131 |
# File 'lib/kbsecret/cli.rb', line 128 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.
152 153 154 155 156 157 |
# File 'lib/kbsecret/cli.rb', line 152 def guard yield rescue => e self.class.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.
175 176 177 178 |
# File 'lib/kbsecret/cli.rb', line 175 def info(msg) info = ENV["NO_COLOR"] ? "Info" : GREEN["Info"] self.class.stderr.puts "#{info}: #{msg}" end |
#prompt(question, echo: true) ⇒ String
Prompt the user for some input.
163 164 165 166 167 168 169 170 |
# File 'lib/kbsecret/cli.rb', line 163 def prompt(question, echo: true) if !echo && self.class.stdin.tty? self.class.stdin.getpass("#{question} ") else self.class.stdout.print "#{question} " self.class.stdin.gets.chomp end end |
#slop(cmds: [], errors: true) ⇒ Slop::Result
This should be called within the block passed to #initialize.
Parse options for a kbsecret
command, adding some default options for
introspection, verbosity, and help output.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/kbsecret/cli.rb', line 70 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 |
#verbose(msg) ⇒ void
This method returns an undefined value.
Print an information message, but only if verbose output has been enabled.
183 184 185 186 |
# File 'lib/kbsecret/cli.rb', line 183 def verbose(msg) return unless @opts.verbose? info msg end |
#warn(msg) ⇒ void
This method returns an undefined value.
Print a warning message unless warnings have been suppressed.
200 201 202 203 204 |
# File 'lib/kbsecret/cli.rb', line 200 def warn(msg) return if @opts.no_warn? warning = ENV["NO_COLOR"] ? "Warning" : YELLOW["Warning"] self.class.stderr.puts "#{warning}: #{msg}" end |