Skip to content

Instantly share code, notes, and snippets.

@dolmen
Last active July 13, 2022 05:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolmen/baf85f57a397c53a2bae065586bba50c to your computer and use it in GitHub Desktop.
Save dolmen/baf85f57a397c53a2bae065586bba50c to your computer and use it in GitHub Desktop.
go-bin-upgrade

go-bin-upgrade

Rebuild the binaries you have built in Go module mode installed in $GOPATH/bin.

Usage: go-bin-upgrade [-n] [-v] bin1 [bin2]...

Examples:

  • go-bin-upgrade -n godoc
  • (cd ~/go/bin ; go-bin-upgrade * )

Limitation: only binaries built in Go module mode can be rebuilt because they embed the module from which they have been built. Check go version -m on the binary for more info.

#!/usr/bin/env bash
# Upgrade Go binaries built in Go module mode and installed in $GOPATH/bin.
# Author: Olivier Mengué
# Source: https://gist.github.com/dolmen/baf85f57a397c53a2bae065586bba50c
set -euo pipefail
IFS=$'\n\t'
update=true
verbose=false
GOBIN="$(go env GOPATH)/bin"
function usage
{
echo "usage: $0 [-n] <file>"
exit 1
}
while getopts 'nv?h' o; do
case "$o" in
n)
update=false
;;
v)
verbose=true
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
for f
do
# path
p="$(go version -m "$GOBIN/$1" 2>/dev/null | sed -n -e '2 s/^ path //p')"
if [[ -n "$p" ]]; then
# mod
m="$(go version -m "$GOBIN/$1" | sed -n -e '3 s/^ mod \([^ ]*\).*/\1/p')"
echo "$f: path=$p module=$m"
if $verbose; then
go version -m "$GOBIN/$1"
fi
$update || continue
( cd "$(go env GOPATH)/src"; go get -t -d "$m" "$p" ) || true
( cd "$(go env GOPATH)/src/$p"; GO111MODULE=on go install )
if $verbose; then
go version -m "$GOBIN/$1"
fi
else
echo "$f: no go module information embeded in binary" >&2
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment