|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2021 The Kube Bind Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Originally copied from |
| 18 | +# https://github.com/kubernetes-sigs/cluster-api-provider-gcp/blob/c26a68b23e9317323d5d37660fe9d29b3d2ff40c/scripts/go_install.sh |
| 19 | +# |
| 20 | +# Installs a Go binary either via "go install" or by cloning and building |
| 21 | +# from source (for modules with replace directives that block go install). |
| 22 | + |
| 23 | +set -o errexit |
| 24 | +set -o nounset |
| 25 | +set -o pipefail |
| 26 | + |
| 27 | +if [[ -z "${1:-}" ]]; then |
| 28 | + echo "must provide module as first parameter" |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +if [[ -z "${2:-}" ]]; then |
| 33 | + echo "must provide binary name as second parameter" |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +if [[ -z "${3:-}" ]]; then |
| 38 | + echo "must provide version as third parameter" |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +if [[ -z "${GOBIN:-}" ]]; then |
| 43 | + echo "GOBIN is not set. Must set GOBIN to install the bin in a specified directory." |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +MODULE="${1}" |
| 48 | +BINARY="${2}" |
| 49 | +VERSION="${3}" |
| 50 | + |
| 51 | +# Extract the repo URL from the module path (everything before /cmd/...) |
| 52 | +# e.g. github.com/kcp-dev/kcp/cmd/kcp -> github.com/kcp-dev/kcp |
| 53 | +REPO_URL="${MODULE}" |
| 54 | +if [[ "${REPO_URL}" == *"/cmd/"* ]]; then |
| 55 | + REPO_URL="${REPO_URL%%/cmd/*}" |
| 56 | +fi |
| 57 | +# Relative path within the repo to the binary (e.g. ./cmd/kcp) |
| 58 | +CMD_PATH="./${MODULE#"${REPO_URL}"/}" |
| 59 | +if [[ "${CMD_PATH}" == "./${MODULE}" ]]; then |
| 60 | + CMD_PATH="." |
| 61 | +fi |
| 62 | + |
| 63 | +mkdir -p "${GOBIN}" |
| 64 | + |
| 65 | +# If the versioned binary already exists, just ensure the symlink and exit. |
| 66 | +if [[ -f "${GOBIN}/${BINARY}-${VERSION}" ]]; then |
| 67 | + ln -sf "${GOBIN}/${BINARY}-${VERSION}" "${GOBIN}/${BINARY}" |
| 68 | + exit 0 |
| 69 | +fi |
| 70 | + |
| 71 | +tmp_dir=$(mktemp -d -t goinstall_XXXXXXXXXX) |
| 72 | +function clean { |
| 73 | + rm -rf "${tmp_dir}" |
| 74 | +} |
| 75 | +trap clean EXIT |
| 76 | + |
| 77 | +rm "${GOBIN}/${BINARY}"* > /dev/null 2>&1 || true |
| 78 | + |
| 79 | +cd "${tmp_dir}" |
| 80 | + |
| 81 | +# Try go install first (works when the module has no replace directives). |
| 82 | +# The || true prevents errexit from killing the script on failure. |
| 83 | +go mod init fake/mod |
| 84 | +if go install -tags tools "${MODULE}@${VERSION}" 2>/dev/null; then |
| 85 | + mv "${GOBIN}/${BINARY}" "${GOBIN}/${BINARY}-${VERSION}" |
| 86 | + ln -sf "${GOBIN}/${BINARY}-${VERSION}" "${GOBIN}/${BINARY}" |
| 87 | + exit 0 |
| 88 | +fi |
| 89 | + |
| 90 | +# Fallback: clone the repo and build from source. |
| 91 | +# This handles modules with replace directives in go.mod. |
| 92 | +echo "go install failed (module likely has replace directives), cloning and building from source..." |
| 93 | +git clone --quiet "https://${REPO_URL}.git" repo |
| 94 | +cd repo |
| 95 | +git checkout --quiet "${VERSION}" |
| 96 | +go build -trimpath -o "${GOBIN}/${BINARY}-${VERSION}" "${CMD_PATH}" |
| 97 | +ln -sf "${GOBIN}/${BINARY}-${VERSION}" "${GOBIN}/${BINARY}" |
0 commit comments