Class: S7::S7Cli::AttributeCopyCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/s7/s7cli/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

Methods included from GetText

#N_, #_, bindtextdomain, included

Constructor Details

#initialize(*args) ⇒ AttributeCopyCommand

Returns a new instance of AttributeCopyCommand.



17
18
19
20
21
22
23
24
25
26
# File 'lib/s7/s7cli/attribute_command.rb', line 17

def initialize(*args)
  super
  @config["copy_command"] = "/usr/bin/pbcopy"
  @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.")),
                StringOption.new("copy_command", _("Copy command.")))
end

Instance Method Details

#run(argv) ⇒ Object

コマンドを実行する。



29
30
31
32
33
34
35
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
# File 'lib/s7/s7cli/attribute_command.rb', line 29

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
  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
  return true
end