Class: KBSecret::CLI::Command::List

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

Overview

The implementation of kbsecret list.

Instance Attribute Summary

Attributes inherited from Abstract

#cli

Instance Method Summary collapse

Methods inherited from Abstract

command_name, config

Constructor Details

#initialize(argv) ⇒ List

Returns a new instance of List.



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

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

      o.string "-s", "--session", "the session to list from", default: :default
      o.string "-t", "--type", "the type of secrets to list", default: nil
      o.bool "-a", "--show-all", "show everything in each secret (i.e. metadata)"
      o.bool "-D", "--sort-date", "sort records by date (oldest to newest)"
      o.bool "-A", "--sort-alphabetical", "sort records by label"
    end

    cli.ensure_type! if cli.opts[:type]
    cli.ensure_session!
  end
end

Instance Method Details

#run!Object

See Also:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kbsecret/cli/command/list.rb', line 41

def run!
  if cli.opts.sort_date?
    @records.sort_by!(&:timestamp)
  elsif cli.opts.sort_alphabetical?
    @records.sort_by!(&:label)
  end

  @records.each do |record|
    puts record.label

    next unless cli.opts.show_all?

    puts <<~DETAIL
      \tType: #{record.type}
      \tLast changed: #{Time.at(record.timestamp)}
      \tRaw data: #{record.data}
    DETAIL
  end
end

#setup!Object

See Also:



29
30
31
# File 'lib/kbsecret/cli/command/list.rb', line 29

def setup!
  @records = cli.session.records TYPE_ALIASES[cli.opts[:type]]
end

#validate!Object

See Also:



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

def validate!
  if cli.opts.sort_date? && cli.opts.sort_alphabetical?
    cli.die "Only one sort flag may be used at once."
  end
end