cleanup verify-gofmt.sh

- clarify comments
- simplify to just use gofmt from path
- setup go env instead of just verify go version
This commit is contained in:
Benjamin Elder 2021-03-07 12:51:33 -08:00
parent e55e41b86a
commit b0144538da

View File

@ -14,10 +14,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# This script checks whether the source codes need to be formatted or not by
# `gofmt`. We should run `hack/update-gofmt.sh` if actually formats them.
# This script checks whether the source code needs to be formatted or not by
# `gofmt`. Run `hack/update-gofmt.sh` to actually format sources.
#
# Note: gofmt output can change between go versions.
#
# Usage: `hack/verify-gofmt.sh`.
# Note: GoFmt apparently is changing @ head...
set -o errexit
set -o nounset
@ -28,12 +30,7 @@ source "${KUBE_ROOT}/hack/lib/init.sh"
cd "${KUBE_ROOT}"
# Prefer bazel's gofmt.
gofmt="external/io_bazel_rules_go_toolchain/bin/gofmt"
if [[ ! -x "${gofmt}" ]]; then
gofmt=$(which gofmt)
kube::golang::verify_go_version
fi
kube::golang::setup_env
find_files() {
find . -not \( \
@ -56,7 +53,7 @@ find_files() {
# formatting (e.g., a file does not parse correctly). Without "|| true" this
# would have led to no useful error message from gofmt, because the script would
# have failed before getting to the "echo" in the block below.
diff=$(find_files | xargs "${gofmt}" -d -s 2>&1) || true
diff=$(find_files | xargs gofmt -d -s 2>&1) || true
if [[ -n "${diff}" ]]; then
echo "${diff}" >&2
echo >&2