| #!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| . /dev/null
|
|
|
|
|
|
|
|
|
| _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}"
|
| GOCACHE="${GOPATH}/.cache/go-build"
|
| GOMODCACHE="${GOPATH}/pkg/mod"
|
|
|
|
|
|
|
|
|
| go.help() {
|
| cat <<EOF
|
| go: GOROOT=${GOROOT}
|
| install : compiles and installs packages
|
| EOF
|
| }
|
|
|
| go.tool() {
|
|
|
| go.env.dev
|
| "${GOVM_EXE}" tool "$@"
|
| }
|
|
|
| go.env.dev() {
|
| if [ -z "$_GO_DEVTOOLS_INSTALLED" ]; then
|
| build_msg INSTALL "[pkg.go.dev] ./go.mod: developer and CI tools"
|
| go.tidy
|
| else
|
| go.vm.ensure
|
| _GO_DEVTOOLS_INSTALLED=1
|
| fi
|
| }
|
|
|
| go.tidy() {
|
| go.vm.ensure
|
| "${GOVM_EXE}" mod tidy
|
| chmod -R u+w "${GOMODCACHE}"
|
| }
|
|
|
| go.clean() {
|
| if ! go.vm.is_installed; then
|
| build_msg CLEAN "[Go] not installed"
|
| return 0
|
| fi
|
| build_msg CLEAN "[Go] drop folders ${GOROOT} and ${GOPATH}"
|
| rm -rf "${GOROOT}" "${GOPATH}"
|
| }
|
|
|
| go.install() {
|
| go.vm.ensure
|
| GOENV="${GOENV}" "${GOVM_EXE}" install "$@"
|
|
|
|
|
| chmod -R u+w "${GOMODCACHE}"
|
| }
|
|
|
| go.os() {
|
| local OS
|
| case "$(command uname -a)xx" in
|
| Linux\ *) OS=linux ;;
|
| Darwin\ *) OS=darwin ;;
|
| FreeBSD\ *) OS=freebsd ;;
|
| CYGWIN* | MSYS* | MINGW*) OS=windows ;;
|
| *) die 42 "OS is unknown: $(command uname -a)" ;;
|
| esac
|
| echo "${OS}"
|
| }
|
|
|
| go.arch() {
|
| local ARCH
|
| case "$(command uname -m)" in
|
| "x86_64") ARCH=amd64 ;;
|
| "aarch64") ARCH=arm64 ;;
|
| "armv6" | "armv7l") ARCH=armv6l ;;
|
| "armv8") ARCH=arm64 ;;
|
| .*386.*) ARCH=386 ;;
|
| ppc64*) ARCH=ppc64le ;;
|
| *) die 42 "ARCH is unknown: $(command uname -m)" ;;
|
| esac
|
| echo "${ARCH}"
|
| }
|
|
|
|
|
|
|
|
|
| go.vm.ensure() {
|
| if ! go.vm.is_installed; then
|
|
|
| go.vm.install
|
| fi
|
| }
|
|
|
| go.vm.is_installed() {
|
|
|
| [[ -f "${GOROOT}/bin/go" ]]
|
| }
|
|
|
|
|
| go.vm.install() {
|
|
|
|
|
|
|
|
|
|
|
| local version dest fname sha size tmp
|
| version="${1:-$GOVERSION}"
|
| dest="${2:-$GOROOT}"
|
|
|
| info_msg "Install Go in ${dest}"
|
|
|
|
|
|
|
| pyenv.install
|
|
|
|
|
| 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() {
|
|
|
|
|
|
|
| "${PY_ENV_BIN}/python" <<EOF
|
| import sys, json, requests
|
| resp = requests.get("${_GO_DL_URL}/?mode=json&include=all")
|
| for ver in json.loads(resp.text):
|
| if not ver['stable']:
|
| continue
|
| for f in ver['files']:
|
| if f['kind'] != 'archive' or not f['size'] or not f['sha256'] or len(f['os']) < 2:
|
| continue
|
| print(" %(version)-10s|%(os)-8s|%(arch)-8s|%(filename)-30s|%(size)-10s|%(sha256)s" % f)
|
| EOF
|
| }
|
|
|
| go.vm.version() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| "${PY_ENV_BIN}/python" - "$@" <<EOF
|
| import sys, json, requests
|
| resp = requests.get("${_GO_DL_URL}/?mode=json&include=all")
|
| for ver in json.loads(resp.text):
|
| if ver['version'] != sys.argv[1]:
|
| continue
|
| for f in ver['files']:
|
| if (f['kind'] != sys.argv[2] or f['os'] != sys.argv[3] or f['arch'] != sys.argv[4]):
|
| continue
|
| for x in sys.argv[5:]:
|
| print(f[x])
|
| sys.exit(0)
|
| sys.exit(42)
|
| EOF
|
| }
|
|
|