Module: Mutineer::Triage
- Defined in:
- lib/mutineer/triage.rb
Overview
Prints .mutineer.yml ignore: entries for survivors in a JSON report,
and for each copy dropped as a repeat of one of those survivors (#374).
An entry still ignores only its own id. The command writes to stdout and
does not edit files. The user pastes the lines under ignore:.
Constant Summary collapse
- USAGE =
Usage line for a bad invocation.
"Usage: mutineer triage REPORT.json --reason TEXT (--id ID ... | --all)"
Class Method Summary collapse
-
.group_for(row) ⇒ Array(String, Array<String>)?
private
One survivor and its copy ids, or nil when the row has no id.
-
.run(argv, out: $stdout, err: $stderr) ⇒ Integer
Reads argv, prints the entries, and returns the process exit code.
-
.survivor_groups(path, err) ⇒ Array<Array(String, Array<String>)>?
Each survivor id and the dropped-copy ids listed on it, in report order.
-
.survivor_ids(path, err) ⇒ Array<String>?
Survivor ids from a JSON report, in report order.
Class Method Details
.group_for(row) ⇒ Array(String, Array<String>)?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
One survivor and its copy ids, or nil when the row has no id.
147 148 149 150 151 152 153 154 |
# File 'lib/mutineer/triage.rb', line 147 def self.group_for(row) return unless row.is_a?(Hash) && row["id"].is_a?(String) && !row["id"].empty? raw = row["copies"] copies = raw.is_a?(Array) ? raw : [] copies = copies.select { |id| id.is_a?(String) && !id.empty? && id != row["id"] }.uniq [row["id"], copies] end |
.run(argv, out: $stdout, err: $stderr) ⇒ Integer
Reads argv, prints the entries, and returns the process exit code. 0 is success. 2 is a usage error.
23 24 25 26 27 28 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mutineer/triage.rb', line 23 def self.run(argv, out: $stdout, err: $stderr) reason = nil ids = [] all = false parser = OptionParser.new do |o| o. = USAGE o.on("--reason TEXT") { |text| reason = text } o.on("--id ID") { |id| ids << id } o.on("--all") { all = true } end begin parser.parse!(argv) rescue OptionParser::ParseError => e err.puts "mutineer: #{e.}" return 2 end report = argv.shift if argv.any? err.puts "mutineer: triage takes one report file" return 2 end if report.nil? || report.empty? err.puts "mutineer: triage requires a JSON report file" return 2 end text = reason.to_s.strip if reason.nil? || text.empty? err.puts "mutineer: triage requires --reason TEXT" return 2 end if all && !ids.empty? err.puts "mutineer: triage takes --id or --all, not both" return 2 end if !all && ids.empty? err.puts "mutineer: triage requires --id ID or --all" return 2 end groups = survivor_groups(report, err) return 2 if groups.nil? by_id = groups.to_h copy_owner = {} groups.each { |id, copies| copies.each { |copy| copy_owner[copy] ||= id } } chosen = all ? groups.map(&:first) : ids missing = chosen.reject { |id| by_id.key?(id) || copy_owner.key?(id) } unless missing.empty? err.puts "mutineer: triage id is not a survivor or one of its copies: #{missing.join(', ')}" return 2 end seen_owners = {} seen_entry_ids = {} entries = [] chosen.uniq.each do |id| owner = by_id.key?(id) ? id : copy_owner[id] next if seen_owners[owner] seen_owners[owner] = true ([owner] + by_id[owner]).each do |entry_id| next if seen_entry_ids[entry_id] seen_entry_ids[entry_id] = true entries << { "id" => entry_id, "reason" => text } end end if entries.empty? err.puts "mutineer: triage has nothing to paste under ignore:" return 0 end body = YAML.dump(entries).sub(/\A---\n/, "") out.write(body.end_with?("\n") ? body : "#{body}\n") 0 end |
.survivor_groups(path, err) ⇒ Array<Array(String, Array<String>)>?
Each survivor id and the dropped-copy ids listed on it, in report order.
A copies value that is not a list counts as none. Nil when the file
cannot be used, after a message on err.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mutineer/triage.rb', line 120 def self.survivor_groups(path, err) unless File.file?(path) err.puts "mutineer: triage report not found: #{path}" return nil end doc = JSON.parse(File.read(path, encoding: "UTF-8")) rows = doc.is_a?(Hash) ? doc["survivors"] : nil unless rows.is_a?(Array) err.puts "mutineer: triage report has no survivors list" return nil end rows.filter_map { |row| group_for(row) } rescue JSON::ParserError => e err.puts "mutineer: triage report is not JSON: #{e.}" nil rescue SystemCallError err.puts "mutineer: triage report cannot be read: #{path}" nil end |
.survivor_ids(path, err) ⇒ Array<String>?
Survivor ids from a JSON report, in report order. Nil when the file
cannot be used, after a message on err.
108 109 110 111 |
# File 'lib/mutineer/triage.rb', line 108 def self.survivor_ids(path, err) groups = survivor_groups(path, err) groups&.map(&:first) end |