~whereswaldon/gover

32b7004a1610185ec91587a8640cfaca50b40e75 — Chris Waldon 6 months ago c84e9b2
feat: support installing latest version
2 files changed, 87 insertions(+), 37 deletions(-)

M README.md
M gover
M README.md => README.md +21 -11
@@ 8,34 8,44 @@ welcome.

## Prerequisites

You must have a conventional `PREFIX` directory structure with its `bin`
directory earlier in your `$PATH` than any other `go` installation.
`gover` assumes a POSIX environment with `grep`, `sed`, `curl`, and `coreutils`.

Pretty much every linux distro creates this for you at `~/.local`, which is
the default installation directory. Make sure you update your `$PATH` so
that `~/.local/bin` is towards the beginning.
## User install (recommended)

## Install
Ensure `$HOME/.local/bin` is towards the beginning of your `$PATH`.

Download the script and put it into any `bin` directory that is in your `$PATH`.
If you're using the default installation prefix, `~/.local/bin` will do.

```
curl --tlsv1.2 -o ~/.local/bin/gover https://git.sr.ht/~whereswaldon/gover/blob/main/gover
curl --tlsv1.2 -o ~/.local/bin/gover https://git.sr.ht/~whereswaldon/gover/blob/main/gover && \
chmod +x ~/.local/bin/gover
```

## System install (requires sudo)

```
sudo curl --tlsv1.2 -o /usr/local/bin/gover https://git.sr.ht/~whereswaldon/gover/blob/main/gover && \
sudo chmod +x /usr/local/bin/gover
```

You will need to invoke `gover` as `sudo gover` each time you use it.

## Usage

```
gover <minor> <patch>
gover <version>
```

This will activate go 1.`minor`.`patch` (installing it if needed).
This will activate the specified version of `go` (installing it if needed).

You can specify `latest` as a version to get the latest available version.

If no version is specified, `latest` is assumed.

## Examples

```
gover # assume latest and install
gover latest # explicitly request latest
gover 14 6 # install and activate go 1.14.6
gover 13 14 # install and activate go 1.13.14
gover 14 6 # activate go 1.14.6 (already installed)

M gover => gover +66 -26
@@ 1,5 1,6 @@
#!/bin/sh
# SPDX-License-Identifier: Unlicense OR MIT
# Source: git.sr.ht/~whereswaldon/gover

set -eu



@@ 8,26 9,34 @@ current_os() {
}

SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
MINOR=${1:-14}
PATCH=${2:-6}
TARGET_VERSION=${1:-latest}
PREFIX=${PREFIX:-$(dirname "$SCRIPT_DIR")}
mkdir -p "$PREFIX"/lib
GOOS=$(current_os)
GOARCH=amd64 # TODO: find a portable way to determine this without go installed

major() {
    echo "$1" | cut -d. -f1
}
minor() {
    echo "$1" | cut -d. -f2 || 0
}
patch() {
    echo "$1" | cut -d. -f3 || 0
}

# version_dir emits the directory into which a particular go
# version should be installed
version_dir() {
    if [ "$2" = "0" ]; then
        echo "$PREFIX/lib/go-1.$1"
        return 0
    fi
    echo "$PREFIX/lib/go-1.$1.$2"
    target=$1
    echo "$PREFIX/lib/go-$target"
}

# version_installed returns whether or not a particular version
# of go is already installed
version_installed() {
    if [ -e "$(version_dir "$1" "$2")" ]; then
    target=$1
    if [ -e "$(version_dir "$target")" ]; then
        return 0
    fi
    return 1


@@ 35,23 44,19 @@ version_installed() {

# install_version installs a particular version of go
install_version() {
    MINOR=$1
    PATCH=$2
    LIB_DIR=$(version_dir "$MINOR" "$PATCH")
    FILENAME=go1.$MINOR.$PATCH.$GOOS-$GOARCH.tar.gz
    if [ "$PATCH" = "0" ]; then
    FILENAME=go1.$MINOR.$GOOS-$GOARCH.tar.gz
    fi
    target=$1
    LIB_DIR=$(version_dir "$target")
    FILENAME=go$target.$GOOS-$GOARCH.tar.gz
    DL_PATH=$LIB_DIR/../$FILENAME
    mkdir -p "$LIB_DIR"
    if ! curl -L -o "$DL_PATH" "https://golang.org/dl/$FILENAME"; then
            echo "Couldn't download Go 1.$MINOR.$PATCH. Are you sure it exists?"
            echo "Couldn't download Go $target. Are you sure it exists?"
            rm "$DL_PATH"
            rm -rf "$LIB_DIR"
            return 1
    fi
    if ! tar --strip-components=1 -xzC "$LIB_DIR" -f "$DL_PATH"; then
            echo "Couldn't download Go 1.$MINOR.$PATCH. Are you sure it exists?"
            echo "Couldn't uncompress Go $target."
            rm "$DL_PATH"
            rm -rf "$LIB_DIR"
            return 1


@@ 62,19 67,54 @@ install_version() {
# activate_version symlinks a particular version of go to be the active
# version
activate_version() {
    MINOR=$1
    PATCH=$2
    ln -svnf "$(version_dir "$1" "$2")/bin/go" "$PREFIX/bin/go"
    target=$1
    ln -svnf "$(version_dir "$target")/bin/go" "$PREFIX/bin/go"
}

# latest_version emits the latest version of go on stdout
latest_version() {
    versions=$(curl -q https://api.github.com/repos/golang/go/git/refs/tags | grep -e "\"ref\":" | grep -e "go" | grep -v -e "beta" -e "rc" | sed -E 's/.*go([[:digit:]]+(.[[:digit:]]+){0,2}).*/\1/')
    highest_major=0
    highest_minor=0
    highest_patch=0
    for version in $versions; do
        major=$(major "$version")
        if [ "$major" -gt "$highest_major" ]; then
            highest_major=$major
        fi
    done
    for version in $versions; do
        major=$(major "$version")
        minor=$(minor "$version")
        if [ "$major" -eq "$highest_major" ] && [ "$minor" -gt "$highest_minor" ]; then
            highest_minor=$minor
        fi
    done
    for version in $versions; do
        major=$(major "$version")
        minor=$(minor "$version")
        patch=$(patch "$version")
        if [ "$major" -eq "$highest_major" ] && [ "$minor" -eq "$highest_minor" ] && [ "$patch" -gt "$highest_patch" ]; then
            highest_patch=$patch
        fi
    done
    if [ "$highest_minor" -eq 0 ] && [ "$highest_patch" -eq 0 ]; then echo "$highest_major"; return 0; fi
    if [ "$highest_patch" -eq 0 ]; then echo "$highest_major.$highest_minor"; return 0; fi
    echo "$highest_major.$highest_minor.$highest_patch"
}

gover() {
    MINOR=$1
    PATCH=$2
    if ! version_installed "$MINOR" "$PATCH"; then
        install_version "$MINOR" "$PATCH"
    target="$1"
    if [ "$target" = "latest" ]; then target=$(latest_version); fi
    if [ "$target" = "-h" ] || [ "$target" = "--help" ]; then
        printf "Usage: gover [version|latest]\n\nExamples:\n\tgover 1.15\n\tgover 1.7.1\n\tgover latest\n\tgover\n"
        return 1
    fi
    if ! version_installed "$target"; then
        install_version "$target"
    fi
    activate_version "$MINOR" "$PATCH"
    activate_version "$target"
    go version
}

gover "$MINOR" "$PATCH"
gover "$TARGET_VERSION"