Class: KBSecret::CLI::Command::Cp

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

Overview

The implementation of kbsecret cp.

Instance Attribute Summary

Attributes inherited from Abstract

#cli

Instance Method Summary collapse

Methods inherited from Abstract

command_name, config, #setup!, #validate!

Constructor Details

#initialize(argv) ⇒ Cp

Returns a new instance of Cp.



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

def initialize(argv)
  super(argv) do |cli|
    cli.slop do |o|
      o.banner = "        Usage:\n          kbsecret cp [options] <source> <destination> <record [record ...]>\n      HELP\n\n      o.bool \"-f\", \"--force\", \"force copying (ignore overwrites)\"\n      o.bool \"-m\", \"--move\", \"delete the record after copying\"\n    end\n\n    cli.dreck do\n      string :src_sess\n      string :dst_sess\n      list :string, :labels\n    end\n  end\nend\n"

Instance Method Details

#run!Object

See Also:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kbsecret/cli/command/cp.rb', line 29

def run!
  src_sess = KBSecret::Session[cli.args[:src_sess]]
  dst_sess = KBSecret::Session[cli.args[:dst_sess]]

  selected_records = src_sess.records.select { |r| cli.args[:labels].include?(r.label) }
  cli.die "No such record(s)." if selected_records.empty?

  overlaps = dst_sess.record_labels & selected_records.map(&:label)

  # the code below actually handles the overwriting if necessary, but we fail early here
  # for friendliness and to avoid half-copying the selected records
  unless overlaps.empty? || cli.opts.force?
    cli.die "Refusing to overwrite existing record(s) without --force."
  end

  selected_records.each do |record|
    dst_sess.import_record(record, overwrite: cli.opts.force?)
    src_sess.delete_record(record.label) if cli.opts.move?
  end
end