class KBSecret::CLI::Command::StashEdit

The implementation of `kbsecret stash-edit`.

Public Class Methods

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

              o.string "-s", "--session", "the session to search in", default: :default
              o.bool "-b", "--base64", "base64 decode the file before editing, and encode it after"
            end

            cli.dreck do
              string :label
            end

            cli.ensure_session!
          end
        end

Public Instance Methods

run!() click to toggle source

@see Command::Abstract#run!

# File lib/kbsecret/cli/command/stash_edit.rb, line 43
def run!
  tempfile = Tempfile.new(@record.label)
  contents = cli.opts.base64? ? Base64.decode64(@record.text) : @record.text

  tempfile.write(contents)
  tempfile.flush

  system "#{ENV["EDITOR"]} #{tempfile.path}"

  tempfile.rewind

  contents     = cli.opts.base64? ? Base64.encode64(tempfile.read) : tempfile.read
  @record.text = contents
ensure
  tempfile.close
  tempfile&.unlink
end
setup!() click to toggle source

@see Command::Abstract#setup!

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

@see Command::Abstract#validate!

# File lib/kbsecret/cli/command/stash_edit.rb, line 37
def validate!
  cli.die "Missing $EDITOR." unless ENV["EDITOR"]
  cli.die "No such unstructured record." unless @record && @record.type == :unstructured
end