Skip to content

Instantly share code, notes, and snippets.

@izgeri
Last active July 10, 2018 02:47
Show Gist options
  • Save izgeri/99eaafbbaed5d6a5fac592cb2597b1d8 to your computer and use it in GitHub Desktop.
Save izgeri/99eaafbbaed5d6a5fac592cb2597b1d8 to your computer and use it in GitHub Desktop.
Bash script to run golint in Docker and output warnings in checkstyle XML format
#!/bin/bash
# this script assumes you have an existing `MY_IMAGE` Docker image with your code in `$project_root`
# you will also need to update the `/path/to/my/project/` with your actual project path
# since golint doesn't ignore the vendor dir, you may want to add multiple `golint /path/...`
# lines to the `docker run` command to check the dirs with your project golang code
# the result is output to golint.xml
# accepts an input string of style errs and outputs XML
output_xml() {
echo "<checkstyle>"
if [[ ! -z "$1" ]]; then
current_file=""
# loop through each file with errors
while IFS= read -r err_line; do
file_and_line_length=$(expr index "$err_line" " ")-2
file_and_line=${err_line:0:file_and_line_length}
IFS=":" read -r -a file_data <<< "$file_and_line"
file=${file_data[0]:$root_dir_length}
line=${file_data[1]}
error=${err_line:$file_and_line_length+2}
if [ "$current_file" != "$file" ]; then
if [[ ! -z "$current_file" ]]; then
echo " </file>"
fi
echo " <file name=\"$file\">"
current_file=$file
fi
echo " <error line=\"$line\" message=\"$error\"></error>"
done <<< "$1"
echo " </file>"
fi
echo "</checkstyle>"
}
project_root="/path/to/my/project/"
root_dir_length=${#project_root}
style_errs=$(docker run \
--rm \
MY_IMAGE \
bash -c "
go get -u golang.org/x/lint/golint
golint /path/to/my/project/dir/...
")
output_xml "$style_errs" 2>&1 | tee ./golint.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment