|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2023 The Kubernetes Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +set -o errexit |
| 17 | +set -o nounset |
| 18 | +set -o pipefail |
| 19 | + |
| 20 | +if [ -z "${1}" ]; then |
| 21 | + echo "must provide module as first parameter" |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +if [ -z "${2}" ]; then |
| 26 | + echo "must provide binary name as second parameter" |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +if [ -z "${3}" ]; then |
| 31 | + echo "must provide version as third parameter" |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +if [ -z "${GOBIN}" ]; then |
| 36 | + echo "GOBIN is not set. Must set GOBIN to install the bin in a specified directory." |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +rm -f "${GOBIN}/${2}"* || true |
| 41 | + |
| 42 | +ORIGINAL_WORKDIR="$(pwd)" |
| 43 | +TMP_MODULE_DIR="${2}.tmp" |
| 44 | + |
| 45 | +# Create TMP_MODULE_DIR to create a go module for building the binary. |
| 46 | +rm -r "${TMP_MODULE_DIR}" || true |
| 47 | +mkdir -p "${TMP_MODULE_DIR}" |
| 48 | +cd "${TMP_MODULE_DIR}" |
| 49 | + |
| 50 | +# Initialize a go module and place a tools.go file for building the binary. |
| 51 | +go mod init "tools" |
| 52 | +# Set require for "sigs.k8s.io/cluster-api" to let go resolve the tools version via the CAPI version. |
| 53 | +go mod edit -replace "sigs.k8s.io/controller-runtime/tools/setup-envtest=github.com/sbueringer/controller-runtime/tools/[email protected]" |
| 54 | + |
| 55 | +# Create go file which imports the required package and resolve dependencies. |
| 56 | +cat << EOF > tools.go |
| 57 | +//go:build tools |
| 58 | +// +build tools |
| 59 | +package tools |
| 60 | +
|
| 61 | +import ( |
| 62 | + _ "${1}" |
| 63 | +) |
| 64 | +EOF |
| 65 | + |
| 66 | +go mod tidy |
| 67 | + |
| 68 | +# Build the binary. |
| 69 | +go build -tags=tools -o "${GOBIN}/${2}-${3}" "${1}" |
| 70 | + |
| 71 | +# Get back to the original directory and cleanup the temporary directory. |
| 72 | +cd "${ORIGINAL_WORKDIR}" |
| 73 | +rm -r "${TMP_MODULE_DIR}" |
| 74 | + |
| 75 | +# Link the unversioned name to the versioned binary. |
| 76 | +ln -sf "${GOBIN}/${2}-${3}" "${GOBIN}/${2}" |
0 commit comments