From 647bd10852cfc5d4778403d0b1730dfd44b21b2a Mon Sep 17 00:00:00 2001 From: Stathi C Date: Tue, 12 Dec 2023 15:08:17 -0600 Subject: [PATCH] go.mod: add toolchain support (#355) * add toolchain support * missing parens * fix grep failures * add auto install flags --- libexec/goenv | 18 ++++++++++-------- libexec/goenv-version-file-read | 11 ++++++++++- test/goenv-version-file-read.bats | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/libexec/goenv b/libexec/goenv index 8437c8db..e80d59c2 100755 --- a/libexec/goenv +++ b/libexec/goenv @@ -13,8 +13,11 @@ if [ -n "$GOENV_DEBUG" ]; then fi abort() { - { if [ "$#" -eq 0 ]; then cat - - else echo "goenv: $*" + { + if [ "$#" -eq 0 ]; then + cat - + else + echo "goenv: $*" fi } >&2 exit 1 @@ -79,7 +82,6 @@ else fi export GOENV_DIR - shopt -s nullglob bin_path="$(abs_dirname "$0")" @@ -106,24 +108,24 @@ export GOENV_HOOK_PATH shopt -u nullglob if [[ -z ${@} ]] && [[ $GOENV_AUTO_INSTALL == 1 ]]; then - set -- "install" + set -- "install" $GOENV_AUTO_INSTALL_FLAGS fi command="$1" case "$command" in -"" ) +"") { goenv---version goenv-help } | abort ;; --v | --version ) +-v | --version) exec goenv---version ;; --h | --help ) +-h | --help) exec goenv-help ;; -* ) +*) if [ "$command" = "shell" ] && [ -z "${GOENV_SHELL}" ]; then echo 'eval "$(goenv init -)" has not been executed.' echo "Please read the installation instructions in the README.md at github.com/go-nv/goenv" diff --git a/libexec/goenv-version-file-read b/libexec/goenv-version-file-read index 0cc61c7d..dd36d37b 100755 --- a/libexec/goenv-version-file-read +++ b/libexec/goenv-version-file-read @@ -11,7 +11,16 @@ if [ ! -f "$VERSION_FILE" ]; then fi if [[ "$(basename $VERSION_FILE)" == "go.mod" ]]; then - versions=($(cat $VERSION_FILE | grep -E "^go [0-9]\\.[0-9]{1,2}(beta|rc)?" | sed 's/go //')) + expression_prefix="" + + has_toolchain=$(cat $VERSION_FILE | { grep -E "^toolchain[ \\t]*" || true; }) + if [[ -n "$has_toolchain" ]]; then + expression_prefix="^toolchain[ \\t]*" + fi + + expression="${expression_prefix}go[ \\t]*[0-9]+\\.[0-9]+(beta|rc)?" + + versions=($(cat $VERSION_FILE | grep -E "${expression}" | sed "s/${expression_prefix}go[ \\t]*//")) else # NOTE: Read the first non-whitespace word from the specified version file. # Be careful not to load it whole in case there's something crazy in it. diff --git a/test/goenv-version-file-read.bats b/test/goenv-version-file-read.bats index 81b42143..e6fcf672 100644 --- a/test/goenv-version-file-read.bats +++ b/test/goenv-version-file-read.bats @@ -50,6 +50,25 @@ IN assert_success "1.11" } +@test "reads go.mod file with toolchain specified in arguments that exists and is not blank" { + cat >go.mod <my-version