Skip to content

Commit

Permalink
Fix build.sh for macOs users (knative#883)
Browse files Browse the repository at this point in the history
* Fix build.sh for macOs users

Fixes knative#882

* cleanup tempdir after installing
  • Loading branch information
rhuss committed Jun 10, 2020
1 parent e1dda56 commit 77b53e2
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 77b53e2

Please sign in to comment.