~whereswaldon/gover

gover/gover -rwxr-xr-x 3.7 KiB
79846e8dChris Waldon feat: link gofmt as well as go 4 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/sh
# SPDX-License-Identifier: Unlicense OR MIT
# Source: git.sr.ht/~whereswaldon/gover

set -eu

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

SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
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() {
    out=$(echo "$1" | cut -d. -f1)
    if [ -z "$out" ]; then echo 0; else echo "$out"; fi
}
minor() {
    out=$(echo "$1" | cut -d. -f2)
    if [ -z "$out" ]; then echo 0; else echo "$out"; fi
}
patch() {
    out=$(echo "$1" | cut -d. -f3)
    if [ -z "$out" ]; then echo 0; else echo "$out"; fi
}

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

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

# install_version installs a particular version of go
install_version() {
    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 $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 uncompress Go $target."
            rm "$DL_PATH"
            rm -rf "$LIB_DIR"
            return 1
    fi
    rm "$DL_PATH"
}

# activate_version symlinks a particular version of go to be the active
# version
activate_version() {
    target=$1
    ln -svnf "$(version_dir "$target")/bin/go" "$PREFIX/bin/go"
    ln -svnf "$(version_dir "$target")/bin/gofmt" "$PREFIX/bin/gofmt"
}

# 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() {
    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 "$target"
    go version
}

gover "$TARGET_VERSION"