add common function to install go tools

Signed-off-by: RainbowMango <renhongcai@huawei.com>
This commit is contained in:
RainbowMango 2021-01-08 18:26:28 +08:00 committed by Kevin Wang
parent c59afde0e6
commit 7f4c61831e
4 changed files with 36 additions and 16 deletions

View File

@ -15,6 +15,8 @@ CONTROLPLANE_SUDO=$(test -w "${CERT_DIR}" || echo "sudo -E")
ROOT_CA_FILE=${CERT_DIR}/server-ca.crt
SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source hack/util.sh
# check whether openssl is installed.
function ensure_openssl {
OPENSSL_BIN=$(command -v openssl)
@ -32,12 +34,7 @@ function ensure-cfssl {
return 0
fi
# Install cfssl tools we need.
TEMP_PATH=$(mktemp -d)
pushd "${TEMP_PATH}" >/dev/null
GO111MODULE=on go get github.com/cloudflare/cfssl/cmd/...@"${CFSSL_VERSION}"
popd >/dev/null
rm -rf "${TEMP_PATH}"
util::install_tools "github.com/cloudflare/cfssl/cmd/..." ${CFSSL_VERSION}
GOPATH=$(go env | grep GOPATH | awk -F '=' '{print $2}'| sed 's/\"//g')
CFSSL_BIN="${GOPATH}/bin/cfssl"

View File

@ -4,13 +4,11 @@ set -o errexit
set -o nounset
set -o pipefail
CONTROLLER_GEN_VERSION="v0.4.1"
CONTROLLER_GEN_PKG="sigs.k8s.io/controller-tools/cmd/controller-gen"
CONTROLLER_GEN_VER="v0.4.1"
# Install tools we need.
TEMP_PATH=$(mktemp -d)
pushd "${TEMP_PATH}" >/dev/null
GO111MODULE=on go get sigs.k8s.io/controller-tools/cmd/controller-gen@"${CONTROLLER_GEN_VERSION}"
popd >/dev/null
rm -rf "${TEMP_PATH}"
source hack/util.sh
util::install_tools ${CONTROLLER_GEN_PKG} ${CONTROLLER_GEN_VER}
controller-gen crd paths=./pkg/apis/... output:crd:dir=./artifacts/deploy

23
hack/util.sh Executable file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
# This function installs a Go tools by 'go get' command.
# Parameters:
# - $1: package name, such as "sigs.k8s.io/controller-tools/cmd/controller-gen"
# - $2: package version, such as "v0.4.1"
# Note:
# Since 'go get' command will resolve and add dependencies to current module, that may update 'go.mod' and 'go.sum' file.
# So we use a temporary directory to install the tools.
function util::install_tools() {
local package="$1"
local version="$2"
temp_path=$(mktemp -d)
pushd "${temp_path}" >/dev/null
GO111MODULE=on go get "${package}"@"${version}"
popd >/dev/null
rm -rf "${temp_path}"
}

View File

@ -5,11 +5,13 @@ set -o nounset
set -o pipefail
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
GOLANGCI_LINT_PKG="github.com/golangci/golangci-lint/cmd/golangci-lint"
GOLANGCI_LINT_VER="v1.32.2"
# TODO(RainbowMango): Pin golangci-lint verison to @v1.32.2
# go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.32.2
cd "${REPO_ROOT}"
source "hack/util.sh"
cd ${REPO_ROOT}
util::install_tools ${GOLANGCI_LINT_PKG} ${GOLANGCI_LINT_VER}
if golangci-lint run; then
echo 'Congratulations! All Go source files have passed staticcheck.'