Class: S7n::S7nCli::AttributeCopyCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/s7n/s7ncli/attribute_command.rb

Overview

機密情報の属性のうち、指定したものの値をクリップボードにコピーするコ マンドを表現する。

Instance Attribute Summary

Attributes inherited from Command

#config, #options, #world

Instance Method Summary collapse

Methods inherited from Command

#aliases, create_instance, #description, #help, #name, #option_parser, split_name_and_argv, #usage

Constructor Details

#initialize(*args) ⇒ AttributeCopyCommand

Returns a new instance of AttributeCopyCommand.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/s7n/s7ncli/attribute_command.rb', line 17

def initialize(*args)
  super
  copy_commands = ["/usr/bin/pbcopy", "/usr/bin/xclip"]
  copy_commands.each do |cmd|
    if File.executable?(cmd)
      @config["copy_command"] = cmd
    end
  end
  @config["inclement_rate"] = true
  @options.push(IntOption.new("i", "id", "ID", _("Entry ID.")),
                StringOption.new("n", "name", _("Attribute name.")),
                BoolOption.new("inclement_rate",
                               _("Inclement the entry's rate after copied.")),
                BoolOption.new("l", "lock",
                               _("Lock screen after copied.")),
                StringOption.new("copy_command", _("Copy command.")))
end

Instance Method Details

#run(argv) ⇒ Object

コマンドを実行する。



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/s7n/s7ncli/attribute_command.rb', line 36

def run(argv)
  option_parser.parse!(argv)
  if argv.length > 0
    config["id"] = argv.shift.to_i
  end
  if argv.length > 0
    config["name"] = argv.shift
  else
    config["name"] = "password"
  end
  if config["id"].nil? || config["name"].nil?
    puts(_("Too few arguments."))
    help
    return true
  end
  puts(_("Copy the attribute value to the clipboard."))
  entry = world.entry_collection.find(config["id"])
  if entry.nil?
    raise NoSuchEntry.new("id" => config["id"])
  end
  attr = entry.get_attribute(config["name"])
  if attr.nil?
    raise NoSuchAttribute.new("id" => config["id"],
                              "name" => config["name"])
  end
  copy_command = @config["copy_command"]
  res, status = Open3.capture2(copy_command,
                               stdin_data: attr.value.to_s,
                               binmode: true)
  if status != 0
    raise CommandFailed.new(copy_command, status)
  end
  if config["inclement_rate"]
    # TODO: undo スタックに操作を追加する。
    entry.rate += 1
    puts(_("Changed rate to %d.") % entry.rate)
    world.changed = true
  end
  if config["lock"]
    sleep(1)
    lock_command = Command.create_instance("lock", world)
    return lock_command.run([])
  end
  return true
end