Skip to content

Commit

Permalink
Install latest patch version if major.minor semantic version provided…
Browse files Browse the repository at this point in the history
…; Add latest command to install (#252)

* Install latest patch version if major.minor semantic version provided. Add latest command to install

* undo formatter on MD file

* re-add old change

* comment re: USE_FAKE_DEFINITIONS

* add unstable feature plus add test fixture for beta version to validate latest
  • Loading branch information
ChronosMasterOfAllTime committed Oct 16, 2022
1 parent 3934658 commit 21cc9a2
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 46 deletions.
2 changes: 1 addition & 1 deletion COMMANDS.md
Expand Up @@ -123,7 +123,7 @@ eval "$(goenv init -)"

## `goenv install`

Install a Go version (using `go-build`). It's required that the version is a known installable definition by `go-build`.
Install a Go version (using `go-build`). It's required that the version is a known installable definition by `go-build`. Alternatively, supply `latest` as an argument to install the latest version available to goenv.

```shell
> goenv install 1.11.1
Expand Down
9 changes: 8 additions & 1 deletion plugins/go-build/bin/go-build
Expand Up @@ -760,7 +760,14 @@ unset IPV6

GO_BUILD_INSTALL_PREFIX="$(abs_dirname "$0")/.."

IFS=: GO_BUILD_DEFINITIONS=($GO_BUILD_DEFINITIONS ${GO_BUILD_ROOT:-$GO_BUILD_INSTALL_PREFIX}/share/go-build)
# USE_FAKE_DEFINITIONS is used for tests to mock the definitions list to our available test fixtures
if [[ $USE_FAKE_DEFINITIONS == "true" ]]; then
DIR_SUFFIX=test/fixtures/definitions
else
DIR_SUFFIX=share/go-build
fi

IFS=: GO_BUILD_DEFINITIONS=($GO_BUILD_DEFINITIONS ${GO_BUILD_ROOT:-$GO_BUILD_INSTALL_PREFIX/$DIR_SUFFIX})
IFS="$OLDIFS"

parse_options "$@"
Expand Down
67 changes: 43 additions & 24 deletions plugins/go-build/bin/goenv-install
Expand Up @@ -2,7 +2,7 @@
#
# Summary: Install a Go version using go-build
#
# Usage: goenv install [-f] [-kvp] <version>
# Usage: goenv install [-f] [-kvp] <version>|latest|unstable
# goenv install [-f] [-kvp] <definition-file>
# goenv install -l|--list
# goenv install --version
Expand Down Expand Up @@ -62,6 +62,14 @@ definitions() {
go-build --definitions | $(type -p ggrep grep | head -1) -F "$query" || true
}

latest_version() {
definitions | grep -oE "^$1\\.([0-9]+)?" | tail -1
}

latest_includes_unstable_version() {
definitions | grep -ioE "^$1\\.?([0-9]+|beta[0-9]+|rc[0-9]+)?" | tail -1
}

indent() {
sed 's/^/ /'
}
Expand All @@ -76,36 +84,36 @@ unset DEBUG
parse_options "$@"
for option in "${OPTIONS[@]}"; do
case "$option" in
"h" | "help" )
"h" | "help")
usage 0
;;
"l" | "list" )
"l" | "list")
echo "Available versions:"
definitions | indent
exit
;;
"f" | "force" )
"f" | "force")
FORCE=true
;;
"s" | "skip-existing" )
"s" | "skip-existing")
SKIP_EXISTING=true
;;
"k" | "keep" )
"k" | "keep")
[ -n "${GOENV_BUILD_ROOT}" ] || GOENV_BUILD_ROOT="${GOENV_ROOT}/sources"
;;
"v" | "verbose" )
"v" | "verbose")
VERBOSE="-v"
;;
"p" | "patch" )
"p" | "patch")
HAS_PATCH="-p"
;;
"g" | "debug" )
"g" | "debug")
DEBUG="-g"
;;
"version" )
"version")
exec go-build --version
;;
* )
*)
usage 1 >&2
;;
esac
Expand All @@ -121,11 +129,23 @@ unset VERSION_NAME
# version is not specified.
DEFINITION="${ARGUMENTS[0]}"

# NOTE: Try to capture semantic versions such as `1.11` which don't have a patch version.
# A patch version of 0 will be added, e.g they'll be changed to `1.11.0`.
if grep -q -E "^[0-9]+\.[0-9]+(\s*)$" <<< ${DEFINITION}; then
echo "Adding patch version 0 to ${DEFINITION}"
DEFINITION="${DEFINITION}.0"
# If latest is supplied, install the latest available (stable) version
if [[ ${DEFINITION} == "latest" ]]; then
LATEST=$(latest_version "[0-9]\\.[0-9]+")
echo "Installing latest version ${LATEST}..."
DEFINITION=$LATEST
# If unstable is supplied, install the latest available (including beta/rc) version
elif [[ ${DEFINITION} == "unstable" ]]; then
LATEST=$(latest_includes_unstable_version "[0-9]\\.[0-9]+")
echo "Installing latest (including unstable) version ${LATEST}..."
DEFINITION=$LATEST
# The latest patch version will be located, e.g if 1.11 is supplied they'll be changed to `1.11.x`.
# NOTE: Try to capture semantic versions such as `1.11` which don't have a patch version and install latest patch.
elif grep -q -E "^[0-9]+\.[0-9]+(\s*)$" <<<${DEFINITION}; then
REGEX=$(echo $DEFINITION | sed s/\\./\\\\./)
LATEST_PATCH=$(latest_version $REGEX)
echo "Using latest patch version $LATEST_PATCH"
DEFINITION="$LATEST_PATCH"
fi

[ -n "$DEFINITION" ] || DEFINITION="$(goenv-local 2>/dev/null || true)"
Expand All @@ -147,13 +167,12 @@ after_install() {
}

OLDIFS="$IFS"
IFS=$'\n' scripts=(`goenv-hooks install`)
IFS=$'\n' scripts=($(goenv-hooks install))
IFS="$OLDIFS"
for script in "${scripts[@]}"; do
source "$script";
source "$script"
done


# Set VERSION_NAME from $DEFINITION, if it is not already set. Then
# compute the installation prefix.
[ -n "$VERSION_NAME" ] || VERSION_NAME="${DEFINITION##*/}"
Expand All @@ -170,8 +189,8 @@ if [ -d "${PREFIX}/bin" ]; then
read -p "continue with installation? (y/N) "

case "$REPLY" in
y* | Y* ) ;;
* ) exit 1 ;;
y* | Y*) ;;
*) exit 1 ;;
esac
elif [ -n "$SKIP_EXISTING" ]; then
# Since we know the go version is already installed, and are opting to
Expand All @@ -195,7 +214,7 @@ fi

# Execute `before_install` hooks.
for hook in "${before_hooks[@]}"; do
eval "$hook";
eval "$hook"
done

# Plan cleanup on unsuccessful installation.
Expand Down Expand Up @@ -236,8 +255,8 @@ if [ "$STATUS" == "2" ]; then
fi

# Execute `after_install` hooks.
for hook in "${after_hooks[@]}";
do eval "$hook";
for hook in "${after_hooks[@]}"; do
eval "$hook"
done

# Run `goenv-rehash` after a successful installation.
Expand Down
17 changes: 17 additions & 0 deletions plugins/go-build/test/fixtures/definitions/1.3beta1
@@ -0,0 +1,17 @@
install_darwin_106_32bit "Go Darwin 10.6 32bit 1.3beta1" "http://localhost:8090/1.3beta1/1.3beta1.tar.gz#d7518d03fc9d7ac1d32c49358594ff6517712c3d3de4f80ebaa3229361f38937"

install_darwin_106_64bit "Go Darwin 10.6 64bit 1.3beta1" "http://localhost:8090/1.3beta1/1.3beta1.tar.gz#d7518d03fc9d7ac1d32c49358594ff6517712c3d3de4f80ebaa3229361f38937"

install_darwin_108_32bit "Go Darwin 10.8 32bit 1.3beta1" "http://localhost:8090/1.3beta1/1.3beta1.tar.gz#d7518d03fc9d7ac1d32c49358594ff6517712c3d3de4f80ebaa3229361f38937"

install_darwin_108_64bit "Go Darwin 10.8 64bit 1.3beta1" "http://localhost:8090/1.3beta1/1.3beta1.tar.gz#d7518d03fc9d7ac1d32c49358594ff6517712c3d3de4f80ebaa3229361f38937"

install_bsd_64bit "Go FreeBSD 64bit 1.3beta1" "http://localhost:8090/1.3beta1/1.3beta1.tar.gz#d7518d03fc9d7ac1d32c49358594ff6517712c3d3de4f80ebaa3229361f38937"

install_bsd_32bit "Go FreeBSD 32bit 1.3beta1" "http://localhost:8090/1.3beta1/1.3beta1.tar.gz#d7518d03fc9d7ac1d32c49358594ff6517712c3d3de4f80ebaa3229361f38937"

install_linux_32bit "Go Linux 32bit 1.3beta1" "http://localhost:8090/1.3beta1/1.3beta1.tar.gz#d7518d03fc9d7ac1d32c49358594ff6517712c3d3de4f80ebaa3229361f38937"

install_linux_64bit "Go Linux 64bit 1.3beta1" "http://localhost:8090/1.3beta1/1.3beta1.tar.gz#d7518d03fc9d7ac1d32c49358594ff6517712c3d3de4f80ebaa3229361f38937"

install_linux_arm_64bit "Go Linux arm 64bit 1.3beta1" "http://localhost:8090/1.3beta1/1.3beta1.tar.gz#d7518d03fc9d7ac1d32c49358594ff6517712c3d3de4f80ebaa3229361f38937"

0 comments on commit 21cc9a2

Please sign in to comment.