Skip to content

Instantly share code, notes, and snippets.

@zephyrtronium
Last active July 12, 2023 18:59
Show Gist options
  • Select an option

  • Save zephyrtronium/d33ff2e72f2794dc1ee5cad901128e69 to your computer and use it in GitHub Desktop.

Select an option

Save zephyrtronium/d33ff2e72f2794dc1ee5cad901128e69 to your computer and use it in GitHub Desktop.
gotools.bash

gotools.bash

gotools.bash prints Go tools that can be updated.

By default, it checks tools in the Go tool's default go install location, so that you can go install $(gotools.bash). (It turns out that this doesn't actually work, though, you need to do something like for cmd in $(gotools.bash); do go install -v $cmd; done instead.) Since the go install location is $GOBIN, you can choose a different location to check by invoking like GOBIN=/usr/local gotools.bash.

There are two senses of "can be updated" that the script checks for. If a tool was built with a different version of Go than go, the script suggests reinstalling it at its current version. Otherwise, if the configured module proxy reports a different version as the latest than the installed version of the command, the script suggests reinstalling it with the reported latest version.

The script directly queries the first element of GOPROXY as the source of truth on latest versions. That module proxy must implement the optional $base/$module/@latest endpoint.

gotools.bash is a script that I wrote in under an hour on a whim. It is not robust. Use with care.

License - 0BSD

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#!/bin/bash
set -e
# Use $GOPROXY for the module proxy to consult.
if [[ -z "$GOPROXY" ]]; then
GOPROXY="$(go env GOPROXY)"
fi
GOPROXY="$(echo $GOPROXY | cut -d, -f1 | cut -d'|' -f1)"
if [[ "$GOPROXY" == direct ]]; then
echo 'This script does not support $GOPROXY=direct.'
exit 1
fi
if [[ -z "$GOBIN" ]]; then
GOBIN="$(go env GOBIN)"
fi
if [[ -z "$GOBIN" ]]; then
GOBIN="$(go env GOPATH)/bin"
fi
if [[ "$GOBIN" == /bin ]]; then
GOBIN="$HOME/go/bin"
fi
MYGOVER=$(go version | cut -d' ' -f3)
for bin in $(go version "$GOBIN" | cut -d: -f1); do
BINGOVER=$(go version $bin | cut -d' ' -f2)
MODINFO=`go version -m $bin | grep $'^\tmod'`
CURRENT=`echo "$MODINFO" | cut -f4`
if [[ $MYGOVER != $BINGOVER ]]; then
echo "$(go version -m $bin | grep $'^\tpath' | cut -f3)@$CURRENT"
continue
fi
MODNAME=`echo "$MODINFO" | cut -f3`
LATEST=`curl -s "$GOPROXY/$MODNAME/@latest" | jq -r '.Version'`
[[ $CURRENT == $LATEST ]] || echo "$(go version -m $bin | grep $'^\tpath' | cut -f3)@$LATEST"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment