class KBSecret::CLI::Command::New
The implementation of `kbsecret new`.
Public Class Methods
new(argv)
click to toggle source
Calls superclass method
KBSecret::CLI::Command::Abstract::new
# File lib/kbsecret/cli/command/new.rb, line 12 def initialize(argv) super(argv) do |cli| cli.slop do |o| o.banner = <<~HELP Usage: kbsecret new [options] <type> <label> HELP o.string "-s", "--session", "the session to contain the record", default: :default o.bool "-f", "--force", "force creation (ignore overwrites, etc.)" o.bool "-e", "--echo", "echo input to tty (only affects interactive input)" o.bool "-G", "--generate", "generate secret fields (interactive only)" o.string "-g", "--generator", "the generator to use for secret fields", default: :default o.bool "-x", "--terse", "read fields from input in a terse format" o.string "-i", "--ifs", "separate terse fields with this string", default: cli.ifs end cli.dreck do string :type string :label end cli.ensure_generator! cli.ensure_type! :argument cli.ensure_session! end end
Public Instance Methods
run!()
click to toggle source
# File lib/kbsecret/cli/command/new.rb, line 57 def run! generator = KBSecret::Generator.new(cli.opts[:generator]) if cli.opts.generate? fields = if cli.opts.terse? cli.stdin.read.chomp.split cli.opts[:ifs] else klass = Record.class_for(@type) klass.external_fields.map do |field| if cli.opts.generate? && klass.sensitive?(field) generator.secret else cli.prompt "#{field.capitalize}?", echo: !klass.sensitive?(field) || cli.opts.echo? end end end cli.session.add_record @type, @label, *fields, overwrite: cli.opts.force? end
setup!()
click to toggle source
# File lib/kbsecret/cli/command/new.rb, line 42 def setup! @label = cli.args[:label] @type = TYPE_ALIASES[cli.args[:type]] end
validate!()
click to toggle source
@see Command::Abstract#validate!
# File lib/kbsecret/cli/command/new.rb, line 48 def validate! # the code below actually handles the overwriting if necessary, but we fail early here # for friendliness and to avoid prompting the user for input unnecessarily if cli.session.record?(@label) && !cli.opts.force? cli.die "Refusing to overwrite a record without --force." end end