Class: KBSecret::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/kbsecret/cli.rb

Overview

An encapsulation of useful methods for kbsecret's CLI.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ CLI

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

Examples:

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

  dreck do
    string :name
  end

  ensure_session!
end

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


33
34
35
36
37
38
39
40
# File 'lib/kbsecret/cli.rb', line 33

def initialize(&block)
  @trailing = ARGV
  @opts = nil
  @args = nil
  instance_eval(&block)
rescue => e
  self.class.die e.to_s.capitalize
end

Instance Attribute Details

#argsDreck::Result (readonly)

Returns the result of trailing argument parsing.

Returns:

  • (Dreck::Result)

    the result of trailing argument parsing



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

def args
  @args
end

#optsSlop::Result (readonly)

Returns the result of option parsing.

Returns:

  • (Slop::Result)

    the result of option parsing



11
12
13
# File 'lib/kbsecret/cli.rb', line 11

def opts
  @opts
end

Class Method Details

.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



97
98
99
100
# File 'lib/kbsecret/cli.rb', line 97

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

.ensure_session(sess_label) ⇒ void

Deprecated.

Use #ensure_session! instead.

Note:

This method does not return if the given session is not configured!

This method returns an undefined value.

Instantiate a session if it exists, and terminate otherwise.

Parameters:

  • sess_label (String, Symbol)

    the session label to instantiate



107
108
109
110
111
# File 'lib/kbsecret/cli.rb', line 107

def ensure_session(sess_label)
  die "Unknown session: '#{sess_label}'." unless Config.session? sess_label

  Session.new label: sess_label
end

.ifsString

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

Returns:

  • (String)

    the field separator



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

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

.slop(cmds: [], errors: false) ⇒ Slop::Result

Deprecated.

Use #initialize instead.

Parse arguments for a kbsecret utility, adding some default options for introspection and help output.

Parameters:

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

    additional commands to print in --introspect-flags

  • errors (Boolean) (defaults to: false)

    whether or not to produce Slop errors

Returns:

  • (Slop::Result)

    the result of argument parsing



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/kbsecret/cli.rb', line 126

def slop(cmds: [], errors: false)
  Slop.parse suppress_errors: !errors do |o|
    yield o

    o.on "-h", "--help" do
      puts o
      exit
    end

    o.on "--introspect-flags" do
      comp = o.options.flat_map(&:flags) + cmds
      puts comp.join "\n"
      exit
    end
  end
end

Instance Method Details

#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



71
72
73
74
75
# File 'lib/kbsecret/cli.rb', line 71

def dreck(errors: true, &block)
  @args = Dreck.parse @trailing, strict: errors do
    instance_eval(&block)
  end
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.

Raises:

  • (RuntimeError)

    if the expected session is not configured.



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

def ensure_session!(where = :option)
  label = where == :option ? @opts[:session] : @args[:session]
  raise "Unknown session: '#{label}'." unless Config.session? label
end

#slop(cmds: [], errors: false) ⇒ 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 and help output.

Parameters:

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

    additional commands to print in --introspect-flags

  • errors (Boolean) (defaults to: false)

    whether or not to produce Slop errors

Returns:

  • (Slop::Result)

    the result of argument parsing



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kbsecret/cli.rb', line 48

def slop(cmds: [], errors: false)
  @opts = Slop.parse suppress_errors: !errors do |o|
    yield o

    o.on "-h", "--help" do
      puts o
      exit
    end

    o.on "--introspect-flags" do
      comp = o.options.flat_map(&:flags) + cmds
      puts comp.join "\n"
      exit
    end
  end

  @trailing = @opts.args
end