lib_go.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. #
  5. # Tools to install and maintain golang [1] binaries & packages.
  6. #
  7. # [1] https://golang.org/doc/devel/release#policy
  8. #
  9. # A simple *helloworld* test with user 'my_user' :
  10. #
  11. # sudo -H adduser my_user
  12. # ./manage go.golang go1.17.3 my_user
  13. # ./manage go.install github.com/go-training/helloworld@latest my_user
  14. # ./manage go.bash my_user
  15. # $ helloword
  16. # Hello World!!
  17. #
  18. # Don't forget to remove 'my_user': sudo -H deluser --remove-home my_user
  19. # shellcheck source=utils/lib.sh
  20. . /dev/null
  21. # configure golang environment
  22. # ----------------------------
  23. [[ -z "${GO_VERSION}" ]] && GO_VERSION="go1.17.3"
  24. GO_DL_URL="https://golang.org/dl"
  25. # implement go functions
  26. # -----------------------
  27. go.help(){
  28. cat <<EOF
  29. go.:
  30. ls : list golang binary archives (stable)
  31. golang : (re-) install golang binary in user's \$HOME/local folder
  32. install : install go package in user's \$HOME/go-apps folder
  33. bash : start bash interpreter with golang environment sourced
  34. EOF
  35. }
  36. go.ls(){
  37. python <<EOF
  38. import sys, json, requests
  39. resp = requests.get("${GO_DL_URL}/?mode=json&include=all")
  40. for ver in json.loads(resp.text):
  41. if not ver['stable']:
  42. continue
  43. for f in ver['files']:
  44. if f['kind'] != 'archive' or not f['size'] or not f['sha256'] or len(f['os']) < 2:
  45. continue
  46. print(" %(version)-10s|%(os)-8s|%(arch)-8s|%(filename)-30s|%(size)-10s|%(sha256)s" % f)
  47. EOF
  48. }
  49. go.ver_info(){
  50. # print informations about a golang distribution. To print filename
  51. # sha256 and size of the archive that fits to your OS and host:
  52. #
  53. # go.ver_info "${GO_VERSION}" archive "$(go.os)" "$(go.arch)" filename sha256 size
  54. #
  55. # usage: go.ver_info <go-vers> <kind> <os> <arch> [filename|sha256|size]
  56. #
  57. # kind: [archive|source|installer]
  58. # os: [darwin|freebsd|linux|windows]
  59. # arch: [amd64|arm64|386|armv6l|ppc64le|s390x]
  60. python - "$@" <<EOF
  61. import sys, json, requests
  62. resp = requests.get("${GO_DL_URL}/?mode=json&include=all")
  63. for ver in json.loads(resp.text):
  64. if ver['version'] != sys.argv[1]:
  65. continue
  66. for f in ver['files']:
  67. if (f['kind'] != sys.argv[2] or f['os'] != sys.argv[3] or f['arch'] != sys.argv[4]):
  68. continue
  69. for x in sys.argv[5:]:
  70. print(f[x])
  71. sys.exit(0)
  72. sys.exit(42)
  73. EOF
  74. }
  75. go.os() {
  76. local OS
  77. case "$(command uname -a)xx" in
  78. Linux\ *) OS=linux ;;
  79. Darwin\ *) OS=darwin ;;
  80. FreeBSD\ *) OS=freebsd ;;
  81. CYGWIN* | MSYS* | MINGW*) OS=windows ;;
  82. *) die 42 "OS is unknown: $(command uname -a)" ;;
  83. esac
  84. echo "${OS}"
  85. }
  86. go.arch() {
  87. local ARCH
  88. case "$(command uname -m)" in
  89. "x86_64") ARCH=amd64 ;;
  90. "aarch64") ARCH=arm64 ;;
  91. "armv6" | "armv7l") ARCH=armv6l ;;
  92. "armv8") ARCH=arm64 ;;
  93. .*386.*) ARCH=386 ;;
  94. ppc64*) ARCH=ppc64le ;;
  95. *) die 42 "ARCH is unknown: $(command uname -m)" ;;
  96. esac
  97. echo "${ARCH}"
  98. }
  99. go.golang() {
  100. # install golang binary in user's $HOME/local folder:
  101. #
  102. # go.golang ${GO_VERSION} ${SERVICE_USER}
  103. #
  104. # usage: go.golang <go-vers> [<username>]
  105. local version fname sha size user userpr
  106. local buf=()
  107. version="${1:-${GO_VERSION}}"
  108. user="${2:-${USERNAME}}"
  109. userpr=" ${_Yellow}|${user}|${_creset} "
  110. rst_title "Install Go in ${user}'s HOME" section
  111. mapfile -t buf < <(
  112. go.ver_info "${version}" archive "$(go.os)" "$(go.arch)" filename sha256 size
  113. )
  114. if [ ${#buf[@]} -eq 0 ]; then
  115. die 42 "can't find info of golang version: ${version}"
  116. fi
  117. fname="${buf[0]}"
  118. sha="${buf[1]}"
  119. size="$(numfmt --to=iec "${buf[2]}")"
  120. info_msg "Download go binary ${fname} (${size}B)"
  121. cache_download "${GO_DL_URL}/${fname}" "${fname}"
  122. pushd "${CACHE}" &> /dev/null
  123. echo "${sha} ${fname}" > "${fname}.sha256"
  124. if ! sha256sum -c "${fname}.sha256" >/dev/null; then
  125. die 42 "downloaded file ${fname} checksum does not match"
  126. else
  127. info_msg "${fname} checksum OK"
  128. fi
  129. popd &> /dev/null
  130. info_msg "install golang"
  131. tee_stderr 0.1 <<EOF | sudo -i -u "${user}" | prefix_stdout "${userpr}"
  132. mkdir -p \$HOME/local
  133. rm -rf \$HOME/local/go
  134. tar -C \$HOME/local -xzf ${CACHE}/${fname}
  135. echo "export GOPATH=\$HOME/go-apps" > \$HOME/.go_env
  136. echo "export PATH=\$HOME/local/go/bin:\\\$GOPATH/bin:\\\$PATH" >> \$HOME/.go_env
  137. EOF
  138. info_msg "test golang installation"
  139. sudo -i -u "${user}" <<EOF
  140. source \$HOME/.go_env
  141. command -v go
  142. go version
  143. EOF
  144. }
  145. go.install() {
  146. # install go package in user's $HOME/go-apps folder:
  147. #
  148. # go.install github.com/go-training/helloworld@lates ${SERVICE_USER}
  149. #
  150. # usage: go.install <package> [<username>]
  151. local package user userpr
  152. package="${1}"
  153. user="${2:-${USERNAME}}"
  154. userpr=" ${_Yellow}|${user}|${_creset} "
  155. if [ -z "${package}" ]; then
  156. die 42 "${FUNCNAME[0]}() - missing argument: <package>"
  157. fi
  158. tee_stderr 0.1 <<EOF | sudo -i -u "${user}" | prefix_stdout "${userpr}"
  159. source \$HOME/.go_env
  160. go install -v ${package}
  161. EOF
  162. }
  163. go.bash() {
  164. # start bash interpreter with golang environment sourced
  165. #
  166. # go.bash ${SERVICE_USER}
  167. #
  168. # usage: go.bash [<username>]
  169. local user
  170. user="${1:-${USERNAME}}"
  171. sudo -i -u "${user}" bash --init-file "~${user}/.go_env"
  172. }
  173. go.version(){
  174. local user
  175. user="${1:-${USERNAME}}"
  176. sudo -i -u "${user}" <<EOF
  177. source \$HOME/.go_env
  178. go version | cut -d' ' -f 3
  179. EOF
  180. }