Skip to content

Instantly share code, notes, and snippets.

@ericfialkowski
Created April 16, 2024 01:14
Show Gist options
  • Save ericfialkowski/6da0a80022bf8ddbd47c919f901611b1 to your computer and use it in GitHub Desktop.
Save ericfialkowski/6da0a80022bf8ddbd47c919f901611b1 to your computer and use it in GitHub Desktop.
Bash Script to Help in Updating Go Dependencies
#!/usr/bin/env bash
#
# Performs the required steps to update minor dependencies
# in a go project
#
if [[ $(git status --porcelain) ]] ; then
echo "================================"
echo "Must be ran on a clean directory"
echo "================================"
echo
git status
exit 1
fi
MAINBRANCH=$(git branch --list -a | grep HEAD | awk -F\/ '{print $NF}')
echo "================================"
echo " Switching to $MAINBRANCH"
echo "================================"
git checkout $MAINBRANCH
echo "================================"
echo " Updating $MAINBRANCH"
echo "================================"
git pull
echo "================================"
echo " Getting updated dependencies"
echo "================================"
go get -t -u ./...
if [[ -z $(git status --porcelain) ]] ; then
echo "================================"
echo " No dependencies to update"
echo "================================"
exit 2
fi
echo "================================"
echo " Tidy the dependencies"
echo "================================"
go mod tidy
if [ -d "./vendor" ]; then
echo "================================"
echo " Updating vendor directory"
echo "================================"
go mod vendor
fi
echo "================================"
echo " Running Tests"
echo "================================"
if [[ ! -x "$(command -v gotestsum)" ]] ; then
go test ./...
else
gotestsum
fi
echo
echo "Commit changes (y/n?)"
read -r answer
if [[ "$answer" =~ ^(y|Y|yes)$ ]] ; then
BRANCH=$(date '+update-deps-%m-%d-%y_%H-%M-%S')
git checkout -b $BRANCH
if [ -d "./vendor" ]; then
git add vendor
fi
git commit -am "Updating minor versions of dependencies"
git push
git checkout $MAINBRANCH
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment