Skip to content

Commit 4579d22

Browse files
committed
partially revert go-install to use unreleased versions
1 parent c9339f1 commit 4579d22

2 files changed

Lines changed: 103 additions & 2 deletions

File tree

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ GORELEASER_VERSION := 2.13.0
3737
GOTESTSUM_VERSION := 1.8.1
3838
HELM_VERSION := 3.18.6
3939
# unlreleased kcp version with vw code for schemas
40-
KCP_VERSION := a45a66683
40+
KCP_VERSION := 301a8f749e7b99a0c81f43b37aa5b5e5ff0fc0b4
4141
KUBE_APPLYCONFIGURATION_GEN_VERSION := v0.32.0
4242
KUBE_CLIENT_GEN_VERSION := v0.32.0
4343
KUBE_INFORMER_GEN_VERSION := v0.32.0
@@ -157,7 +157,11 @@ install-boilerplate:
157157

158158
.PHONY: install-kcp
159159
install-kcp:
160-
@hack/uget.sh https://github.com/kcp-dev/kcp/releases/download/v{VERSION}/kcp_{VERSION}_{GOOS}_{GOARCH}.tar.gz kcp $(KCP_VERSION)
160+
@if echo "$(KCP_VERSION)" | grep -qE '^v?[0-9]+\.[0-9]+'; then \
161+
hack/uget.sh https://github.com/kcp-dev/kcp/releases/download/v{VERSION}/kcp_{VERSION}_{GOOS}_{GOARCH}.tar.gz kcp $(KCP_VERSION); \
162+
else \
163+
GOBIN=$(abspath $(UGET_DIRECTORY)) hack/go-install.sh github.com/kcp-dev/kcp/cmd/kcp kcp $(KCP_VERSION); \
164+
fi
161165

162166
GORELEASER = $(UGET_DIRECTORY)/goreleaser-$(GORELEASER_VERSION)
163167

hack/go-install.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)