Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/usr/local/containerbase/tools/v2/golang.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/bin/bash

function check_tool_requirements () {
check_semver "${TOOL_VERSION}"
if [[ ! "${MAJOR}" || ! "${MINOR}" ]]; then # patch is optional and will default to latest available
echo Invalid version: "${TOOL_VERSION}"
exit 1
fi
}

function prepare_tool() {
# go suggests: git svn bzr mercurial
Expand Down Expand Up @@ -57,20 +64,29 @@ function install_tool () {
bsdtar -C "$(find_tool_path)" -xf "${file}"
else

now=$(date +%Y%m%d%H) # cache for one hour
checksum_file=$(get_from_url "https://go.dev/dl/?mode=json&include=all&_=${now}" "go-versions.${now}.json")

# fix version, only for go 1.20 and below
fversion=${TOOL_VERSION}
if [[ ($MAJOR -lt 1 || ($MAJOR -eq 1 && $MINOR -lt 21)) && "${PATCH}" == "0" ]]; then
fversion="${MAJOR}.${MINOR}"
fi

if [[ ! "${PATCH}" ]]; then
fversion="$(jq -r --arg prefix "$TOOL_VERSION" 'first(.[].version[2:] | select(startswith($prefix)))' < "${checksum_file}")"
if [[ -z "$fversion" ]]; then
echo "Could not resolve version $TOOL_VERSION" >&2
exit 1;
fi
fi

if [[ "${arch}" = "aarch64" ]]; then
arch=arm64
else
arch=amd64
fi

now=$(date +%Y%m%d%H) # cache for one hour
checksum_file=$(get_from_url "https://go.dev/dl/?mode=json&include=all&_=${now}" "go-versions.${now}.json")
expected_checksum="$(jq -r ".[] | select(.version == \"go${fversion}\") | .files[] | select(.os == \"linux\" and .arch == \"${arch}\") | .sha256" < "${checksum_file}")"

file=$(get_from_url \
Expand Down
12 changes: 12 additions & 0 deletions test/golang/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ RUN set -ex; \
RUN set -ex; \
[ "$(command -v mod)" = "/usr/local/bin/mod" ] && echo "works" || exit 1;

#--------------------------------------
# test: golang (automatic patch version selection)
#--------------------------------------
FROM base AS testd

# do not update
RUN install-tool golang 1.20

# go 1.20 is EOL so the latest patch version should stay at 1.20.14
RUN go version | grep "go1.20.14"

#--------------------------------------
# final
#--------------------------------------
Expand All @@ -111,3 +122,4 @@ FROM base
COPY --from=testa /.dummy /.dummy
COPY --from=testb /.dummy /.dummy
COPY --from=testc /.dummy /.dummy
COPY --from=testd /.dummy /.dummy