class KBSecret::CLI::Command::StashFile

The implementation of `kbsecret stash-file`.

Public Class Methods

new(argv) click to toggle source
Calls superclass method KBSecret::CLI::Command::Abstract::new
# File lib/kbsecret/cli/command/stash_file.rb, line 10
        def initialize(argv)
          super(argv) do |cli|
            cli.slop do |o|
              o.banner = <<~HELP
                Usage:
                  kbsecret stash-file <record> [filename]
              HELP

              o.string "-s", "--session", "the session to add to", default: :default
              o.bool "-f", "--force", "force creation (ignore overwrites, etc.)"
              o.bool "-b", "--base64", "encode the file as base64"
              o.bool "-", "--stdin", "read from stdin instead of a filename"
            end

            cli.dreck do
              string :label
              string :filename unless cli.opts.stdin?
            end

            cli.ensure_session!
          end
        end

Public Instance Methods

run!() click to toggle source

@see Command::Abstract#run!

# File lib/kbsecret/cli/command/stash_file.rb, line 51
def run!
  contents = if cli.opts.stdin?
               cli.stdin.read
             else
               File.read(@filename)
             end

  contents = Base64.encode64(contents) if cli.opts.base64?

  cli.session.add_record(:unstructured, @label, contents, overwrite: cli.opts.force?)
end
setup!() click to toggle source

@see Command::Abstract#setup!

# File lib/kbsecret/cli/command/stash_file.rb, line 34
def setup!
  @label = cli.args[:label]
  @filename = cli.args[:filename]
end
validate!() click to toggle source

@see Command::Abstract#validate!

# File lib/kbsecret/cli/command/stash_file.rb, line 40
def validate!
  if cli.session.record?(@label) && !cli.opts.force?
    cli.die "Refusing to overwrite a record without --force."
  end

  if @filename
    cli.die "No such file." unless File.file?(@filename)
  end
end