Class: KBSecret::CLI

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

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.

Deprecated.

see create

Returns a new instance of CLI.



48
49
50
51
# File 'lib/kbsecret/cli.rb', line 48

def initialize
  @argv = ARGV.dup
  guard { yield self }
end

Instance Attribute Details

#argsDreck::Result? (readonly)

Returns the result of trailing argument parsing, if requested via #dreck.

Returns:

  • (Dreck::Result, nil)

    the result of trailing argument parsing, if requested via #dreck



17
18
19
# File 'lib/kbsecret/cli.rb', line 17

def args
  @args
end

#optsSlop::Result? (readonly)

Returns the result of option parsing, if requested via #slop.

Returns:

  • (Slop::Result, nil)

    the result of option parsing, if requested via #slop



13
14
15
# File 'lib/kbsecret/cli.rb', line 13

def opts
  @opts
end

#sessionSession? (readonly)

Returns the session associated with the command, if requested via #ensure_session!.

Returns:



21
22
23
# File 'lib/kbsecret/cli.rb', line 21

def session
  @session
end

Class Method Details

.create {|CLI| ... } ⇒ CLI

Encapsulate both the options and trailing arguments passed to a kbsecret command.

Examples:

cmd = KBSecret::CLI.create do |c|
  c.slop do |o|
    o.string "-s", "--session", "session label"
    o.bool "-f", "--foo", "whatever"
  end

  c.dreck do
    string :name
  end

  c.ensure_session!
end

cmd.opts # => Slop::Result
cmd.args # => Dreck::Result

Yields:

Returns:

  • (CLI)

    the command's initial state



42
43
44
# File 'lib/kbsecret/cli.rb', line 42

def self.create(&block)
  CLI.new(&block)
end

.die(msg) ⇒ void

Note:

This method does not return!

This method returns an undefined value.

Print an error message and terminate.

Parameters:

  • msg (String)

    the message to print



185
186
187
188
# File 'lib/kbsecret/cli.rb', line 185

def die(msg)
  pretty = "#{"Fatal".red}: #{msg}"
  abort pretty
end

.ifsString

Finds a reasonable default field separator by checking the environment first and then falling back to ":".

Returns:

  • (String)

    the field separator



193
194
195
# File 'lib/kbsecret/cli.rb', line 193

def ifs
  ENV["IFS"] || ":"
end

Instance Method Details

#bye(msg) ⇒ void

Note:

This method does not return!

This method returns an undefined value.

Print an informational message via #info and exit successfully.

Parameters:

  • msg (String)

    the message to print



158
159
160
161
# File 'lib/kbsecret/cli.rb', line 158

def bye(msg)
  info msg
  exit
end

#die(msg) ⇒ void

Note:

This method does not return!

This method returns an undefined value.

Print an error message and terminate.

Parameters:

  • msg (String)

    the message to print



175
176
177
178
# File 'lib/kbsecret/cli.rb', line 175

def die(msg)
  pretty = "#{"Fatal".red}: #{msg}"
  abort pretty
end

#dreck(errors: true, &block) ⇒ Object

Note:

If #slop is called, it must be called before this.

Parse trailing arguments for a kbsecret utility, using the elements remaining after options have been removed and interpreted via #slop.

Parameters:

  • errors (Boolean) (defaults to: true)

    whether or not to produce (strict) Dreck errors



87
88
89
90
91
# File 'lib/kbsecret/cli.rb', line 87

def dreck(errors: true, &block)
  @args = Dreck.parse @argv, strict: errors do
    instance_eval(&block)
  end
end

#ensure_generator!(where = :option) ⇒ void

Note:

#slop and #dreck should be called before this, depending on whether options or arguments are being tested for a valid session.

This method returns an undefined value.

Ensure that a generator profile passed in as an option or argument already exists (i.e., is already configured).

Parameters:

  • where (Symbol) (defaults to: :option)

    Where to look for the session label to test. If :option is passed, then the generator is expected to be the value of the --generator option. If :argument is passed, then the type is expected to be in the argument list labeled as :generator by Dreck.



130
131
132
133
# File 'lib/kbsecret/cli.rb', line 130

def ensure_generator!(where = :option)
  gen = where == :option ? @opts[:generator] : @args[:generator]
  Config.generator gen
end

#ensure_session!(where = :option) ⇒ void

Note:

#slop and #dreck should be called before this, depending on whether options or arguments are being tested for a valid session.

This method returns an undefined value.

Ensure that a session passed in as an option or argument already exists (i.e., is already configured).

Parameters:

  • where (Symbol) (defaults to: :option)

    Where to look for the session label to test. If :option is passed, then the session is expected to be the value of the --session option. If :argument is passed, then the session is expected to be in the argument list labeled as :argument by Dreck.



102
103
104
105
# File 'lib/kbsecret/cli.rb', line 102

def ensure_session!(where = :option)
  label = where == :option ? @opts[:session] : @args[:session]
  @session = Session.new label: label
end

#ensure_type!(where = :option) ⇒ void

Note:

#slop and #dreck should be called before this, depending on whether options or arguments are being tested for a valid session.

This method returns an undefined value.

Ensure that a record type passed in as an option or argument is resolvable to a record class.

Parameters:

  • where (Symbol) (defaults to: :option)

    Where to look for the record type to test. If :option is passed, then the type is expected to be the value of the --type option. If :argument is passed, then the type is expected to be in the argument list labeled as :type by Dreck.



116
117
118
119
# File 'lib/kbsecret/cli.rb', line 116

def ensure_type!(where = :option)
  type = where == :option ? @opts[:type] : @args[:type]
  Record.class_for type
end

#guardObject

Note:

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.

Returns:

  • (Object)

    the result of the block



140
141
142
143
144
# File 'lib/kbsecret/cli.rb', line 140

def guard
  yield
rescue => e
  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.

Parameters:

  • msg (String)

    the message to print



149
150
151
152
# File 'lib/kbsecret/cli.rb', line 149

def info(msg)
  return unless @opts.verbose?
  STDERR.puts "#{"Info".green}: #{msg}"
end

#slop(cmds: [], errors: true) ⇒ Slop::Result

Note:

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.

Parameters:

  • cmds (Array<String>) (defaults to: [])

    additional commands to print in --introspect-flags

  • errors (Boolean) (defaults to: true)

    whether or not to produce Slop errors

Returns:

  • (Slop::Result)

    the result of argument parsing



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kbsecret/cli.rb', line 59

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.on "-h", "--help", "show this help message" do
      puts o
      exit
    end

    o.on "--introspect-flags", "dump recognized flags and subcommands" do
      comp = o.options.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.

Parameters:

  • msg (String)

    the message to print



166
167
168
169
# File 'lib/kbsecret/cli.rb', line 166

def warn(msg)
  return if @opts.no_warn?
  STDERR.puts "#{"Warning".yellow}: #{msg}"
end