From 20a193f04c8da00de19993dd67a503d21ef87ac9 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Sun, 13 Jul 2025 14:29:12 +0200 Subject: [mod] add Golang ecosystem to the SearXNG toolchain Signed-off-by: Markus Heiser --- utils/lib_govm.sh | 226 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100755 utils/lib_govm.sh (limited to 'utils/lib_govm.sh') diff --git a/utils/lib_govm.sh b/utils/lib_govm.sh new file mode 100755 index 000000000..c74f4a65a --- /dev/null +++ b/utils/lib_govm.sh @@ -0,0 +1,226 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# Go versions manager to install and maintain golang [1] binaries & packages. +# +# [1] https://golang.org/doc/devel/release#policy + +# shellcheck source=utils/lib.sh +. /dev/null + +# configure golang environment for go.vm +# -------------------------------------- + +_GO_DL_URL="https://go.dev/dl" + +GOVERSION="${GOVERSION:-go$(awk '/^go /{print $2}' "${REPO_ROOT}/go.mod")}" +GOTOOLCHAIN=local + +GOROOT="${REPO_ROOT}/.govm/${GOVERSION}" +GOENV="${GOROOT}/.config/go.env" +GOVM_EXE="${GOROOT}/bin/go" + +GOPATH="${REPO_ROOT}/local/${GOVERSION}" # no support for multiple path names!! +GOCACHE="${GOPATH}/.cache/go-build" +GOMODCACHE="${GOPATH}/pkg/mod" + +# implement go functions +# ----------------------- + +go.help() { + cat < + + local version dest fname sha size tmp + version="${1:-$GOVERSION}" + dest="${2:-$GOROOT}" + + info_msg "Install Go in ${dest}" + + # HINT: the python requirements needed by go.vm.version are taken from the + # developer environment. If it is not yet installed, install it now .. + pyenv.install + + # fetch go version .. + local buf=() + mapfile -t buf < <( + go.vm.version "${version}" archive "$(go.os)" "$(go.arch)" filename sha256 size + ) + if [ ${#buf[@]} -eq 0 ]; then + die 42 "can't find info of golang version: ${version}" + fi + fname="${buf[0]}" + sha="${buf[1]}" + size="$(numfmt --to=iec "${buf[2]}")" + + info_msg "Download go binary ${fname} (${size}B)" + cache_download "${_GO_DL_URL}/${fname}" "${fname}" + + pushd "${CACHE}" &>/dev/null + echo "${sha} ${fname}" >"${fname}.sha256" + if ! sha256sum -c "${fname}.sha256" >/dev/null; then + die 42 "downloaded file ${fname} checksum does not match" + else + info_msg "${fname} checksum OK" + fi + popd &>/dev/null + + info_msg "install golang" + + tmp="$(mktemp -d)" + tar -C "${tmp}" -xzf "${CACHE}/${fname}" + rm -rf "${dest}" + mkdir -p "$(dirname "${dest}")" + mv "${tmp}/go" "${dest}" + + mkdir -p "$(dirname "$GOENV")" + export GOENV + + "${GOVM_EXE}" telemetry off + "${GOVM_EXE}" env -w \ + GOBIN="$GOBIN" \ + GOTOOLCHAIN="$GOTOOLCHAIN" \ + GOCACHE="$GOCACHE" \ + GOPATH="$GOPATH" \ + GOMODCACHE="$GOMODCACHE" + + mkdir -p "${GOMODCACHE}" +} + +go.vm.list() { + + # Go versions manager; list Go versions (stable) + + python < [filename|sha256|size] + # + # kind: [archive|source|installer] + # os: [darwin|freebsd|linux|windows] + # arch: [amd64|arm64|386|armv6l|ppc64le|s390x] + + python - "$@" <