~whereswaldon/gover

ref: 8684e6476e6027d63e8e085041f2b8330be9a8e0 gover/gover -rwxr-xr-x 1.5 KiB
8684e647Chris Waldon feat: support macOS and FreeBSD 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/sh
# SPDX-License-Identifier: Unlicense OR MIT

set -eu

current_os() {
    uname | tr '[:upper:]' '[:lower:]'
}

MINOR=${1:-14}
PATCH=${2:-6}
PREFIX=${PREFIX:-$HOME/.local}
GOOS=$(current_os)
GOARCH=amd64 # TODO: find a portable way to determine this without go installed

# version_dir emits the directory into which a particular go
# version should be installed
version_dir() {
    echo "$PREFIX/lib/go-1.$1.$2"
}

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

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

# activate_version symlinks a particular version of go to be the active
# version
activate_version() {
    MINOR=$1
    PATCH=$2
    ln -svf "../lib/go-1.$MINOR.$PATCH/bin/go" "$PREFIX/bin/go"
}

gover() {
    MINOR=$1
    PATCH=$2
    if ! version_installed "$MINOR" "$PATCH"; then
        install_version "$MINOR" "$PATCH"
    fi
    activate_version "$MINOR" "$PATCH"
    go version
}

gover "$MINOR" "$PATCH"