Merge pull request #5043 from cli/nolint-insert

Add `nolint-insert` script to mark current lint violations as allowed
This commit is contained in:
Mislav Marohnić 2022-01-17 12:35:54 +01:00 committed by GitHub
commit 0a6dc76e5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

24
script/nolint-insert Executable file
View file

@ -0,0 +1,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 ./...