From 9f46def1a89f48713bf5b3c2f58f37cf63c34a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 14 Jan 2022 19:54:09 +0100 Subject: [PATCH] Add `nolint-insert` script to auto-comment lint violations Step 1: mark a function as deprecated Step 2: run `script/nolint-insert` Step 3: all callers of that function now have a `//nolint` directive --- script/nolint-insert | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 script/nolint-insert diff --git a/script/nolint-insert b/script/nolint-insert new file mode 100755 index 000000000..b9e3f9298 --- /dev/null +++ b/script/nolint-insert @@ -0,0 +1,24 @@ +#!/bin/bash +# Usage: script/nolint-insert +# script/nolint-insert 'nolint:staticcheck // ' +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 ./...