From 83b776af468a758e2f2fa77282059fdf22db510f Mon Sep 17 00:00:00 2001 From: Aaron Son Date: Wed, 7 Aug 2019 12:20:25 -0700 Subject: [PATCH] go/utils/repofmt/check_fmt.sh: Add a script to format the repo appropriately. --- go/utils/repofmt/check_fmt.sh | 4 +++- go/utils/repofmt/format_repo.sh | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100755 go/utils/repofmt/format_repo.sh 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