diff --git a/go/utils/repofmt/check_fmt.sh b/go/utils/repofmt/check_fmt.sh index 8522c49e6d..bc7c104ab0 100755 --- a/go/utils/repofmt/check_fmt.sh +++ b/go/utils/repofmt/check_fmt.sh @@ -10,7 +10,7 @@ if [ "$bad_files" != "" ]; then echo "ERROR: The following files do not match goimports output:" echo "$bad_files" echo - echo "Please format the go code in the repository with 'goimports -l -local github.com/liquidata-inc/dolt .'" + echo "Please format the go code in the repository with './utils/repofmt/format_repo.sh'" exit 1 fi @@ -23,5 +23,7 @@ done) if [ "$bad_files" != "" ]; then echo "ERROR: The following files have more than three import groups:" echo "$bad_files" + echo + echo "Please format the go code in the repository with './utils/repofmt/format_repo.sh'" exit 1 fi diff --git a/go/utils/repofmt/format_repo.sh b/go/utils/repofmt/format_repo.sh new file mode 100755 index 0000000000..191aa35d68 --- /dev/null +++ b/go/utils/repofmt/format_repo.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +set -eo pipefail + +script_dir=$(dirname "$0") +cd $script_dir/../.. + +goimports -w -local github.com/liquidata-inc/dolt . + +bad_files=$(find . -name '*.go' | while read f; do + if [[ $(awk '/import \(/{flag=1;next}/\)/{flag=0}flag' < $f | egrep -c '$^') -gt 2 ]]; then + echo $f + fi +done) + +if [ "$bad_files" != "" ]; then + for f in $bad_files; do + awk '/import \(/{flag=1}/\)/{flag=0}flag&&!/^$/||!flag' < "$f" > "$f.bak" + mv "$f.bak" "$f" + done + goimports -w -local github.com/liquidata-inc/dolt . +fi