Class: KBSecret::CLI::Command::StashEdit

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

Overview

The implementation of kbsecret stash-edit.

Instance Attribute Summary

Attributes inherited from Abstract

#cli

Instance Method Summary collapse

Methods inherited from Abstract

command_name, config

Constructor Details

#initialize(argv) ⇒ StashEdit

Returns a new instance of StashEdit.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kbsecret/cli/command/stash_edit.rb', line 11

def initialize(argv)
  super(argv) do |cli|
    cli.slop do |o|
      o.banner = "        Usage:\n          kbsecret stash-edit [options] <record>\n      HELP\n\n      o.string \"-s\", \"--session\", \"the session to search in\", default: :default\n      o.bool \"-b\", \"--base64\", \"base64 decode the file before editing, and encode it after\"\n    end\n\n    cli.dreck do\n      string :label\n    end\n\n    cli.ensure_session!\n  end\nend\n"

Instance Method Details

#run!Object

See Also:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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!Object

See Also:



32
33
34
# File 'lib/kbsecret/cli/command/stash_edit.rb', line 32

def setup!
  @record = cli.session[cli.args[:label]]
end

#validate!Object

See Also:



37
38
39
40
# 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