File: nolint-insert

package info (click to toggle)
gh 2.46.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,548 kB
  • sloc: sh: 227; makefile: 117
file content (24 lines) | stat: -rwxr-xr-x 629 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
# Usage: script/nolint-insert
#        script/nolint-insert 'nolint:staticcheck // <explanation>'
set -e

insert-line() {
  local n=$'\n'
  sed -i.bak "${2}i\\${n}${3}${n}" "$1"
  rm "$1.bak"
}

reverse() {
  awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }'
}

comment="${1}"

golangci-lint run --out-format json | jq -r '.Issues[] | [.Pos.Filename, .Pos.Line, .FromLinter, .Text] | @tsv' | reverse | while IFS=$'\t' read -r filename line linter text; do
  directive="nolint:${linter} // $text"
  [ -z "$comment" ] || directive="$comment"
  insert-line "$filename" "$line" "//${directive}"
done

go fmt ./...