Class: KBSecret::CLI::Command::Env

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

Overview

The implementation of kbsecret env.

Instance Attribute Summary

Attributes inherited from Abstract

#cli

Instance Method Summary collapse

Methods inherited from Abstract

command_name, config

Constructor Details

#initialize(argv) ⇒ Env

Returns a new instance of Env.



8
9
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/env.rb', line 8

def initialize(argv)
  super(argv) do |cli|
    cli.slop do |o|
      o.banner = <<~HELP
        Usage:
          kbsecret env [options] <record [record ...]>
      HELP

      o.string "-s", "--session", "the session to search in", default: :default
      o.bool "-a", "--all", "retrieve all environment records, not just listed ones"
      o.bool "-v", "--value-only", "print only the environment value, not the key"
      o.bool "-n", "--no-export", "print only VAR=val keypairs without `export`"
      o.bool "-u", "--unescape-plus", "escape any pluses in the variable and/or value"
    end

    unless cli.opts.all?
      cli.dreck do
        list :string, :labels
      end
    end

    cli.ensure_session!
  end
end

Instance Method Details

#run!Object

See Also:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kbsecret/cli/command/env.rb', line 50

def run!
  env_output = if cli.opts.no_export?
                 @records.map(&:to_assignment).join(" ")
               elsif cli.opts.value_only?
                 @records.map(&:value).join("\n")
               else
                 @records.map(&:to_export).join("\n")
               end

  env_output.gsub!("\\+", "+") if cli.opts.unescape_plus?

  puts env_output
end

#setup!Object

See Also:



34
35
36
37
38
39
40
41
42
# File 'lib/kbsecret/cli/command/env.rb', line 34

def setup!
  @records = if cli.opts.all?
               cli.session.records :environment
             else
               cli.session.records(:environment).select do |record|
                 cli.args[:labels].include? record.label
               end
             end
end

#validate!Object

See Also:



45
46
47
# File 'lib/kbsecret/cli/command/env.rb', line 45

def validate!
  cli.die "No such record(s)." if @records.empty?
end