Created
April 2, 2024 20:45
-
-
Save ryancragun/0b41d19d18f399743f4f6f0169a4b840 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# A hacky fixer for copyloopvar errors | |
go install github.com/karamaru-alpha/copyloopvar/cmd/copyloopvar@latest | |
cmd="sed" | |
if uname -s | grep Darwin; then | |
cmd="gsed" | |
fi | |
declare -A visited | |
while IFS= read -r copy; do | |
# file:line:indent: The copy of the 'for' variable "test" can be deleted (Go 1.22+) | |
file=$(cut -d ":" -f 1 <<< "$copy") | |
line=$(cut -d ":" -f 2 <<< "$copy") | |
if ! [[ $line =~ ^[0-9]+$ ]]; then | |
# Ignore non-useful output | |
continue | |
fi | |
# Since our files likely have more than a single instance that needs to be updated we need | |
# to track how often we visit the file so we can decrement the line index each time. | |
# This relies on the fact that copyloopvar is output ordered. | |
visited_times="${visited["$file"]}" | |
(( decr="0" )) | |
if [ "$visited_times" != "" ]; then | |
(( decr="$visited_times" )) | |
fi | |
(( remove = line - decr )) | |
echo $cmd -i "${remove}d" "$file" | |
$cmd -i "${remove}d" "$file" | |
(( visited["$file"]++ )) | |
done < <(go vet -vettool="$(which copyloopvar)" ./... 2>&1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment