Skip to content

Instantly share code, notes, and snippets.

@vearutop
Created October 12, 2018 16:09
Show Gist options
  • Save vearutop/a09b706a1bc0762409a178f95b2d6563 to your computer and use it in GitHub Desktop.
Save vearutop/a09b706a1bc0762409a178f95b2d6563 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
default_description="something special"
while [ 1 ]
do
missing_comments=$(golint `go list ./... | grep -v /vendor/` | grep 'comment' | grep 'exported')
echo "$missing_comments"
test -z "${missing_comments// }" && exit
test ! -z "${missing_comments// }" && echo "Fixing missing comments on exported methods, structs, interfaces and consts" && echo "$missing_comments" | \
while read report; do
file=$(echo $report | cut -d':' -f1);
file_normalized=$(echo $file | tr -cd '[[:alnum:]]')
eval "((count_$file_normalized++))"
# One line will be added to file
line_changed_counter=$(eval "echo \$count_$file_normalized")
line_temp=$(echo $report | cut -d':' -f2);
((line_temp--))
line=$((line_temp + line_changed_counter))
res=$(awk "NR==$line,/(^[^\/\/])/{ printf \"%s:%s\n\", NR, \$0 }" $file);
if [[ $res == *"func"* ]]; then
method=$(echo "$res" | tail -1);
if [[ "$method" =~ .*func[[:space:]][A-Z] ]]; then
method=$(echo "$method" | awk -F')' '{ print $1 }' | cut -d'(' -f1 | cut -d' ' -f2)
else
method=$(echo "$method" | awk -F')' '{ print $2 }' | cut -d'(' -f1 | cut -d' ' -f2)
fi
else
method=$(echo $res | grep -oE '[A-Z]\w+' | head -1)
fi
echo "$res"
echo $method
comment=$(echo "$res" | grep "// $method" | tail -1);
if [[ -z "${comment// }" ]]; then
if [[ -z "${desc// }" ]]; then
desc=$default_description
fi
comment=$(echo "// $method is $desc");
else
line_comment=$(echo $comment | cut -d':' -f1);
awk "NR!~/^$line_comment\$/{ print > \"$file\" }" $file
# One line was removed
eval "((count_$file_normalized--))"
comment=$(echo $comment | cut -d':' -f2-)
fi
perl -i -lpe "print '$comment' if \$. == $line" $file;
done
//exit
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment