Class: KBSecret::CLI::Command::DumpFields

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

Overview

The implementation of kbsecret dump-fields.

Instance Attribute Summary

Attributes inherited from Abstract

#cli

Instance Method Summary collapse

Methods inherited from Abstract

command_name, config

Constructor Details

#initialize(argv) ⇒ DumpFields

Returns a new instance of DumpFields.



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

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

      o.string "-s", "--session", "the session to search in", default: :default
      o.bool "-x", "--terse", "output in field<sep>value format"
      o.string "-i", "--ifs", "separate terse pairs with this string", default: cli.ifs
    end

    cli.dreck do
      string :label
    end

    cli.ensure_session!
  end
end

Instance Method Details

#run!Object

See Also:



40
41
42
43
44
45
46
47
48
49
# File 'lib/kbsecret/cli/command/dump_fields.rb', line 40

def run!
  field_values = @record.data_fields.map { |f| @record.send f }
  field_pairs  = @record.data_fields.zip(field_values)

  if cli.opts.terse?
    puts field_pairs.map { |f, v| "#{f}#{cli.opts[:ifs]}#{v}" }.join "\n"
  else
    puts field_pairs.map { |f, v| "#{f}: #{v}" }.join "\n"
  end
end

#setup!Object

See Also:



30
31
32
# File 'lib/kbsecret/cli/command/dump_fields.rb', line 30

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

#validate!Object

See Also:



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

def validate!
  cli.die "No such record." unless @record
end