Created
August 14, 2025 19:14
-
-
Save JohnDDuncanIII/bdc0bdb9f9e79acbfff67ba229b194a1 to your computer and use it in GitHub Desktop.
upgrade_go.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # upgrade_go: updates all Go binaries in $GOPATH/bin | |
| # Valid parameters: | |
| # -v : verbose output (shows all packages) | |
| # -vv: very verbose output (shows all packages and separates outdated vs current) | |
| upgrade_go() { | |
| # https://go.dev/ref/mod#go-install | |
| # https://github.com/nao1215/gup | |
| # https://github.com/Gelio/go-global-update | |
| # https://github.com/shurcooL/binstale | |
| # https://github.com/kortschak/ugbt | |
| # https://github.com/golang/go/issues/71351 | |
| # https://github.com/golang/go/issues/50261 | |
| # https://github.com/golang/go/issues/45546 | |
| # https://github.com/golang/go/issues/65687 | |
| # https://github.com/mvdan/dotfiles/blob/master/.bin/go-insts | |
| local RED='\033[31m' | |
| local GREEN='\033[32m' | |
| local RESET='\033[0m' | |
| # -v argument | |
| local verbose=0 | |
| local verboseverbose=0 | |
| for arg in "$@" | |
| do | |
| [ "$arg" = "-v" ] && verbose=1 | |
| [ "$arg" = "-vv" ] && verbose=1 && verboseverbose=1 | |
| done | |
| local needs_upgrade_arr=() | |
| local outdated_arr=() | |
| local current_arr=() | |
| for pkg in "$GOPATH"/bin/* | |
| do | |
| # install_path=$(go version -m "$pkg" 2>/dev/null | sed -nE 's/^[[:space:]]*path[[:space:]]+//p') | |
| # mod_path=$(go version -m "$pkg" 2>/dev/null | awk '/^[[:space:]]*path[[:space:]]+/{print $2 "@latest"; exit}') | |
| read -r install_path mod_path installed_version <<< "$( | |
| go version -m "$pkg" 2>/dev/null | awk ' | |
| /^[[:space:]]*path[[:space:]]+/ {install_path=$2 "@latest"} | |
| /^[[:space:]]*mod[[:space:]]+/ {mod_path=$2; installed_version=$3} | |
| END | |
| {print install_path, mod_path, installed_version} | |
| ' | |
| )" | |
| [ -z "$mod_path" ] && [ -z "$installed_version" ] && continue | |
| current_version=$(go list -m -f "{{.Version}}" "$mod_path@latest" 2>/dev/null) | |
| [ -z "$current_version" ] && current_version="$installed_version" | |
| local COLOR="$GREEN" | |
| if \ | |
| [ "$(printf '%s\n%s\n' "$installed_version" "$current_version" | sort -V | head -n1)" = "$installed_version" ] \ | |
| && [ "$installed_version" != "$current_version" ] | |
| then | |
| COLOR="$RED" | |
| needs_upgrade_arr+=("$install_path") | |
| fi | |
| local info_str | |
| info_str=$(printf "${COLOR}%s${RESET} -> %s (installed: %s, current: %s)\n" "$(basename "$pkg")" "$install_path" "$installed_version" "$current_version") | |
| if [ "$COLOR" = "$RED" ] || [ "$verbose" -eq 1 ] | |
| then | |
| if [ "$verboseverbose" -eq 1 ] | |
| then | |
| if [ "$COLOR" = "$RED" ] | |
| then | |
| outdated_arr+=("$info_str") | |
| else | |
| current_arr+=("$info_str") | |
| fi | |
| fi | |
| echo "$info_str" | |
| fi | |
| done | |
| if [ "${#needs_upgrade_arr[@]}" -gt 0 ] | |
| then | |
| if [ "$verboseverbose" -eq 1 ] | |
| then | |
| printf "\nCurrent:\n" | |
| for current in "${current_arr[@]}" | |
| do | |
| echo "$current" | |
| done | |
| printf "\nOutdated:\n" | |
| for outdated in "${outdated_arr[@]}" | |
| do | |
| echo "$outdated" | |
| done | |
| fi | |
| for install_path in "${needs_upgrade_arr[@]}" | |
| do | |
| # go install "$install_path" | |
| true | |
| done | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment