lib_govm.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. #
  4. # Go versions manager to install and maintain golang [1] binaries & packages.
  5. #
  6. # [1] https://golang.org/doc/devel/release#policy
  7. # shellcheck source=utils/lib.sh
  8. . /dev/null
  9. # configure golang environment for go.vm
  10. # --------------------------------------
  11. _GO_DL_URL="https://go.dev/dl"
  12. GOVERSION="${GOVERSION:-go$(awk '/^go /{print $2}' "${REPO_ROOT}/go.mod")}"
  13. GOTOOLCHAIN=local
  14. GOROOT="${REPO_ROOT}/.govm/${GOVERSION}"
  15. GOENV="${GOROOT}/.config/go.env"
  16. GOVM_EXE="${GOROOT}/bin/go"
  17. GOPATH="${REPO_ROOT}/local/${GOVERSION}" # no support for multiple path names!!
  18. GOCACHE="${GOPATH}/.cache/go-build"
  19. GOMODCACHE="${GOPATH}/pkg/mod"
  20. # implement go functions
  21. # -----------------------
  22. go.help() {
  23. cat <<EOF
  24. go: GOROOT=${GOROOT}
  25. install : compiles and installs packages
  26. EOF
  27. }
  28. go.tool() {
  29. # shortcut for "go tool .." in the Go environment
  30. go.env.dev
  31. "${GOVM_EXE}" tool "$@"
  32. }
  33. go.env.dev() {
  34. if [ -z "$_GO_DEVTOOLS_INSTALLED" ]; then
  35. build_msg INSTALL "[pkg.go.dev] ./go.mod: developer and CI tools"
  36. go.tidy
  37. else
  38. go.vm.ensure
  39. _GO_DEVTOOLS_INSTALLED=1
  40. fi
  41. }
  42. go.tidy() {
  43. go.vm.ensure
  44. "${GOVM_EXE}" mod tidy
  45. chmod -R u+w "${GOMODCACHE}"
  46. }
  47. go.clean() {
  48. if ! go.vm.is_installed; then
  49. build_msg CLEAN "[Go] not installed"
  50. return 0
  51. fi
  52. build_msg CLEAN "[Go] drop folders ${GOROOT} and ${GOPATH}"
  53. rm -rf "${GOROOT}" "${GOPATH}"
  54. }
  55. go.install() {
  56. go.vm.ensure
  57. GOENV="${GOENV}" "${GOVM_EXE}" install "$@"
  58. # not sure why, but go installs some files without setting the write access
  59. # for the file owner
  60. chmod -R u+w "${GOMODCACHE}"
  61. }
  62. go.os() {
  63. local OS
  64. case "$(command uname -a)xx" in
  65. Linux\ *) OS=linux ;;
  66. Darwin\ *) OS=darwin ;;
  67. FreeBSD\ *) OS=freebsd ;;
  68. CYGWIN* | MSYS* | MINGW*) OS=windows ;;
  69. *) die 42 "OS is unknown: $(command uname -a)" ;;
  70. esac
  71. echo "${OS}"
  72. }
  73. go.arch() {
  74. local ARCH
  75. case "$(command uname -m)" in
  76. "x86_64") ARCH=amd64 ;;
  77. "aarch64") ARCH=arm64 ;;
  78. "armv6" | "armv7l") ARCH=armv6l ;;
  79. "armv8") ARCH=arm64 ;;
  80. .*386.*) ARCH=386 ;;
  81. ppc64*) ARCH=ppc64le ;;
  82. *) die 42 "ARCH is unknown: $(command uname -m)" ;;
  83. esac
  84. echo "${ARCH}"
  85. }
  86. # Go version management (go.vm)
  87. # -----------------------------
  88. go.vm.ensure() {
  89. if ! go.vm.is_installed; then
  90. # shellcheck disable=SC2119
  91. go.vm.install
  92. fi
  93. }
  94. go.vm.is_installed() {
  95. # is true if "go" command is installed
  96. [[ -f "${GOROOT}/bin/go" ]]
  97. }
  98. # shellcheck disable=SC2120
  99. go.vm.install() {
  100. # Go versions manager; to install Go at arbitrary place:
  101. #
  102. # usage: go.vm.install <version> <dest>
  103. local version dest fname sha size tmp
  104. version="${1:-$GOVERSION}"
  105. dest="${2:-$GOROOT}"
  106. info_msg "Install Go in ${dest}"
  107. # HINT: the python requirements needed by go.vm.version are taken from the
  108. # developer environment. If it is not yet installed, install it now ..
  109. pyenv.install
  110. # fetch go version ..
  111. local buf=()
  112. mapfile -t buf < <(
  113. go.vm.version "${version}" archive "$(go.os)" "$(go.arch)" filename sha256 size
  114. )
  115. if [ ${#buf[@]} -eq 0 ]; then
  116. die 42 "can't find info of golang version: ${version}"
  117. fi
  118. fname="${buf[0]}"
  119. sha="${buf[1]}"
  120. size="$(numfmt --to=iec "${buf[2]}")"
  121. info_msg "Download go binary ${fname} (${size}B)"
  122. cache_download "${_GO_DL_URL}/${fname}" "${fname}"
  123. pushd "${CACHE}" &>/dev/null
  124. echo "${sha} ${fname}" >"${fname}.sha256"
  125. if ! sha256sum -c "${fname}.sha256" >/dev/null; then
  126. die 42 "downloaded file ${fname} checksum does not match"
  127. else
  128. info_msg "${fname} checksum OK"
  129. fi
  130. popd &>/dev/null
  131. info_msg "install golang"
  132. tmp="$(mktemp -d)"
  133. tar -C "${tmp}" -xzf "${CACHE}/${fname}"
  134. rm -rf "${dest}"
  135. mkdir -p "$(dirname "${dest}")"
  136. mv "${tmp}/go" "${dest}"
  137. mkdir -p "$(dirname "$GOENV")"
  138. export GOENV
  139. "${GOVM_EXE}" telemetry off
  140. "${GOVM_EXE}" env -w \
  141. GOBIN="$GOBIN" \
  142. GOTOOLCHAIN="$GOTOOLCHAIN" \
  143. GOCACHE="$GOCACHE" \
  144. GOPATH="$GOPATH" \
  145. GOMODCACHE="$GOMODCACHE"
  146. mkdir -p "${GOMODCACHE}"
  147. }
  148. go.vm.list() {
  149. # Go versions manager; list Go versions (stable)
  150. "${PY_ENV_BIN}/python" <<EOF
  151. import sys, json, requests
  152. resp = requests.get("${_GO_DL_URL}/?mode=json&include=all")
  153. for ver in json.loads(resp.text):
  154. if not ver['stable']:
  155. continue
  156. for f in ver['files']:
  157. if f['kind'] != 'archive' or not f['size'] or not f['sha256'] or len(f['os']) < 2:
  158. continue
  159. print(" %(version)-10s|%(os)-8s|%(arch)-8s|%(filename)-30s|%(size)-10s|%(sha256)s" % f)
  160. EOF
  161. }
  162. go.vm.version() {
  163. # Print information about a Go distribution. To print filename sha256 and
  164. # size of the archive that fits to your OS and host:
  165. #
  166. # go.ver_info "${GOVERSION}" archive "$(go.os)" "$(go.arch)" filename sha256 size
  167. #
  168. # usage: go.vm.version <go-vers> <kind> <os> <arch> [filename|sha256|size]
  169. #
  170. # kind: [archive|source|installer]
  171. # os: [darwin|freebsd|linux|windows]
  172. # arch: [amd64|arm64|386|armv6l|ppc64le|s390x]
  173. "${PY_ENV_BIN}/python" - "$@" <<EOF
  174. import sys, json, requests
  175. resp = requests.get("${_GO_DL_URL}/?mode=json&include=all")
  176. for ver in json.loads(resp.text):
  177. if ver['version'] != sys.argv[1]:
  178. continue
  179. for f in ver['files']:
  180. if (f['kind'] != sys.argv[2] or f['os'] != sys.argv[3] or f['arch'] != sys.argv[4]):
  181. continue
  182. for x in sys.argv[5:]:
  183. print(f[x])
  184. sys.exit(0)
  185. sys.exit(42)
  186. EOF
  187. }