diff --git a/hack/build.sh b/hack/build.sh index 7dc3e43d0c..b61e4c9cfd 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -16,9 +16,6 @@ set -o pipefail -[[ ! -v REPO_ROOT_DIR ]] && REPO_ROOT_DIR="$(git rev-parse --show-toplevel)" -readonly REPO_ROOT_DIR - source_dirs="cmd pkg test lib" # Store for later @@ -113,27 +110,16 @@ go_fmt() { find $(echo $source_dirs) -name "*.go" -print0 | xargs -0 gofmt -s -w } -# Run a go tool, installing it first if necessary. -# Parameters: $1 - tool package/dir for go get/install. -# $2 - tool to run. -# $3..$n - parameters passed to the tool. +# Run a go tool, get it first if necessary. run_go_tool() { local tool=$2 local install_failed=0 - if [[ -z "$(which ${tool})" ]]; then - local action=get - [[ $1 =~ ^[\./].* ]] && action=install - # Avoid running `go get` from root dir of the repository, as it can change go.sum and go.mod files. - # See discussions in https://github.com/golang/go/issues/27643. - if [[ ${action} == "get" && $(pwd) == "${REPO_ROOT_DIR}" ]]; then - local temp_dir="$(mktemp -d)" - # Swallow the output as we are returning the stdout in the end. - pushd "${temp_dir}" > /dev/null 2>&1 - GOFLAGS="" go ${action} "$1" || install_failed=1 - popd > /dev/null 2>&1 - else - GOFLAGS="" go ${action} "$1" || install_failed=1 - fi + if [ -z "$(which ${tool})" ]; then + local temp_dir="$(mktemp -d)" + pushd "${temp_dir}" > /dev/null 2>&1 + GOFLAGS="" go get "$1" || install_failed=1 + popd > /dev/null 2>&1 + rm -rf "${temp_dir}" fi (( install_failed )) && return ${install_failed} shift 2