Class: KBSecret::CLI::Command::Rm

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

Overview

The implementation of kbsecret rm.

Instance Attribute Summary

Attributes inherited from Abstract

#cli

Instance Method Summary collapse

Methods inherited from Abstract

command_name, config

Constructor Details

#initialize(argv) ⇒ Rm

Returns a new instance of Rm.



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

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

      o.string "-s", "--session", "the session containing the record", default: :default
      o.bool "-i", "--interactive", "ask for confirmation before deleting"
    end

    cli.dreck do
      list :string, :labels
    end

    cli.ensure_session!
  end
end

Instance Method Details

#run!Object

See Also:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kbsecret/cli/command/rm.rb', line 43

def run!
  $VERBOSE = nil # tty-prompt blasts us with irrelevant warnings on 2.4

  tty = TTY::Prompt.new

  confirm = if cli.opts.interactive?
              tty.yes?("Delete '#{@selected_records.join(", ")}'?")
            else true
            end

  @selected_records.each { |r| cli.session.delete_record(r.label) } if confirm
end

#setup!Object

See Also:



31
32
33
34
35
# File 'lib/kbsecret/cli/command/rm.rb', line 31

def setup!
  @selected_records = cli.session.records.select do |record|
    cli.args[:labels].include? record.label
  end
end

#validate!Object

See Also:



38
39
40
# File 'lib/kbsecret/cli/command/rm.rb', line 38

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