2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
9 # Copyright 2024 Ian Kelling
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
15 # http://www.apache.org/licenses/LICENSE-2.0
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
24 # usage: no args, run to install latest go.
26 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
28 shopt -s inherit_errexit
2>/dev
/null ||
: # ignore fail in bash < 4.4
29 source /usr
/local
/lib
/bash-bear
36 m
() { printf "$pre %s\n" "$*"; "$@"; }
37 e
() { printf "$pre %s\n" "$*"; }
38 err
() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $0: $*" >&2; }
41 if [[ ! -d /a
/opt
/go.git
]]; then
44 git clone
--bare https
://go.googlesource.com
/go
53 yesterday
=$
(( $
(date +%s
) - 60*60*24 ))
54 fetch_mtime
=$
(stat
-c %Y FETCH_HEAD
)
55 if [[ ! $fetch_mtime ]]; then
56 err failed to get output from stat
-c %Y FETCH_HEAD
59 # no need to check for updates on go more than once a day
60 if (( fetch_mtime
> yesterday
)); then
64 # update will get overwritten if we dont do it on central host.
65 if [[ -e /a
/bin
/bash_unpublished
/source-state
]]; then
66 source /a
/bin
/bash_unpublished
/source-state
67 if [[ $HOSTNAME != "$HOST2" ]]; then
76 for f
in go
*.
tar.gz
; do
78 done |
sort -Vr |
head -n1
87 touch ~
/.local
/gofetch
89 vers
=($
(git tag |
sort -Vr |
grep -E '^go[0-9.]+$' |
head))
93 arch
=$
(dpkg
--print-architecture)
94 f
=${ver}.linux-
${arch}.
tar.gz
96 timestamp
=$
(stat
-c %Y
$f)
100 m wget
-nv -N https
://dl.google.com
/go
/$f || ret
=$?
103 # 8 is return code for 404. for a new release, they might not have a binary out yet
104 if [[ $ret == 8 ]]; then
109 new_timestamp
=$
(stat
-c %Y
$f)
110 if [[ $timestamp != $new_timestamp ]]; then
115 if $install ||
[[ ! -e /usr
/local
/go
/bin
/go
]]; then
116 m sudo
rm -rf /usr
/local
/go
117 m sudo
tar -C /usr
/local -xzf $f
120 for x
in go
*.
tar.gz
; do
121 if [[ $x == $f ]]; then continue; fi