Class: KBSecret::CLI::Command::New
- Defined in:
- lib/kbsecret/cli/command/new.rb
Overview
The implementation of kbsecret new
.
Instance Attribute Summary
Attributes inherited from Abstract
Instance Method Summary collapse
-
#initialize(argv) ⇒ New
constructor
A new instance of New.
- #run! ⇒ Object
- #setup! ⇒ Object
- #validate! ⇒ Object
Methods inherited from Abstract
Constructor Details
#initialize(argv) ⇒ New
Returns a new instance of New.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/kbsecret/cli/command/new.rb', line 12 def initialize(argv) super(argv) do |cli| cli.slop do |o| o. = " Usage:\n kbsecret new [options] <type> <label>\n HELP\n\n o.string \"-s\", \"--session\", \"the session to contain the record\", default: :default\n o.bool \"-f\", \"--force\", \"force creation (ignore overwrites, etc.)\"\n o.bool \"-e\", \"--echo\", \"echo input to tty (only affects interactive input)\"\n o.bool \"-G\", \"--generate\", \"generate secret fields (interactive only)\"\n o.string \"-g\", \"--generator\", \"the generator to use for secret fields\",\n default: :default\n o.bool \"-x\", \"--terse\", \"read fields from input in a terse format\"\n o.string \"-i\", \"--ifs\", \"separate terse fields with this string\", default: cli.ifs\n end\n\n cli.dreck do\n string :type\n string :label\n end\n\n cli.ensure_generator!\n cli.ensure_type! :argument\n cli.ensure_session!\n end\nend\n" |
Instance Method Details
#run! ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# 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! ⇒ Object
42 43 44 45 |
# File 'lib/kbsecret/cli/command/new.rb', line 42 def setup! @label = cli.args[:label] @type = TYPE_ALIASES[cli.args[:type]] end |
#validate! ⇒ Object
48 49 50 51 52 53 54 |
# 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 |