Fix build.sh for macOs users (#883)

* Fix build.sh for macOs users

Fixes #882

* cleanup tempdir after installing
This commit is contained in:
Roland Huß 2020-06-09 15:42:32 +02:00 committed by GitHub
parent dfd65db9ab
commit ff350fb6a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 21 deletions

View File

@ -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