Class: KBSecret::CLI::Command::StashFile

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

Overview

The implementation of kbsecret stash-file.

Instance Attribute Summary

Attributes inherited from Abstract

#cli

Instance Method Summary collapse

Methods inherited from Abstract

command_name, config

Constructor Details

#initialize(argv) ⇒ StashFile

Returns a new instance of StashFile.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Method Details

#run!Object

See Also:



51
52
53
54
55
56
57
58
59
60
61
# 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!Object

See Also:



34
35
36
37
# File 'lib/kbsecret/cli/command/stash_file.rb', line 34

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

#validate!Object

See Also:



40
41
42
43
44
45
46
47
48
# 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