diff --git a/Makefile b/Makefile index 80132336..eb0fa3f7 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ test.shell: utils/lib.sh \ utils/lib_install.sh \ utils/lib_static.sh \ + utils/lib_go.sh \ utils/filtron.sh \ utils/searx.sh \ utils/morty.sh \ diff --git a/manage b/manage index 0c7b04fb..b61b4a19 100755 --- a/manage +++ b/manage @@ -9,6 +9,9 @@ source "$(dirname "${BASH_SOURCE[0]}")/utils/lib.sh" # shellcheck source=utils/lib_static.sh source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_static.sh" +# shellcheck source=utils/lib_go.sh +source "$(dirname "${BASH_SOURCE[0]}")/utils/lib_go.sh" + # config PYOBJECTS="searx" @@ -91,6 +94,7 @@ themes.: pygments.: less : build LESS files for pygments EOF + go.help static_help } diff --git a/utils/lib_go.sh b/utils/lib_go.sh new file mode 100755 index 00000000..314204e1 --- /dev/null +++ b/utils/lib_go.sh @@ -0,0 +1,214 @@ +#!/usr/bin/env bash +# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*- +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# Tools to install and maintain golang [1] binaries & packages. +# +# [1] https://golang.org/doc/devel/release#policy +# +# A simple *helloworld* test with user 'my_user' : +# +# sudo -H adduser my_user +# ./manage go.golang go1.17.3 my_user +# ./manage go.install github.com/go-training/helloworld@latest my_user +# ./manage go.bash my_user +# $ helloword +# Hello World!! +# +# Don't forget to remove 'my_user': sudo -H deluser --remove-home my_user + +# shellcheck source=utils/lib.sh +. /dev/null + +# shellcheck disable=SC2034 +declare main_cmd + +# configure golang environment +# ---------------------------- + +[[ -z "${GO_VERSION}" ]] && GO_VERSION="go1.17.3" + +GO_DL_URL="https://golang.org/dl" + +# implement go functions +# ----------------------- + +go.help(){ + cat < [filename|sha256|size] + # + # kind: [archive|source|installer] + # os: [darwin|freebsd|linux|windows] + # arch: [amd64|arm64|386|armv6l|ppc64le|s390x] + + python3 - "$@" < [] + + local version fname sha size user userpr + local buf=() + + version="${1:-${GO_VERSION}}" + user="${2:-${USERNAME}}" + userpr=" ${_Yellow}|${user}|${_creset} " + + rst_title "Install Go in ${user}'s HOME" section + + mapfile -t buf < <( + go.ver_info "${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" + tee_stderr 0.1 < \$HOME/.go_env +echo "export PATH=\$HOME/local/go/bin:\\\$GOPATH/bin:\\\$PATH" >> \$HOME/.go_env +EOF + info_msg "test golang installation" + sudo -i -u "${user}" < [] + + local package user userpr + + package="${1}" + user="${2:-${USERNAME}}" + userpr=" ${_Yellow}|${user}|${_creset} " + + if [ -z "${package}" ]; then + die 42 "${FUNCNAME[0]}() - missing argument: " + fi + tee_stderr 0.1 <] + + local user + user="${1:-${USERNAME}}" + sudo -i -u "${user}" bash --init-file "~${user}/.go_env" +} + +go.version(){ + local user + user="${1:-${USERNAME}}" + sudo -i -u "${user}" <