Skip to content

Instantly share code, notes, and snippets.

@poom
Created May 23, 2025 04:23
Show Gist options
  • Save poom/e1043dd371393a9c622f210fa637aae9 to your computer and use it in GitHub Desktop.
Save poom/e1043dd371393a9c622f210fa637aae9 to your computer and use it in GitHub Desktop.
Script to update go dependencies
#!/bin/bash
set -e
# --- Prerequisite Checks ---
if ! command -v go &> /dev/null; then
echo "Error: Go command-line tool is not installed or not in PATH."
echo "Please install Go: https://golang.org/doc/install"
exit 1
fi
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed or not in PATH."
echo "Please install jq. Examples:"
echo " macOS: brew install jq"
echo " Debian/Ubuntu: sudo apt-get install jq"
echo " Other: Check https://stedolan.github.io/jq/download/"
exit 1
fi
if [ ! -f "go.mod" ]; then
echo "Error: go.mod file not found in the current directory."
echo "Please run this script from the root of your Go module."
exit 1
fi
# --- Get Go Module Data ---
GO_MOD_JSON_DATA=$(go mod edit -json)
if [ -z "$GO_MOD_JSON_DATA" ]; then
echo "Error: 'go mod edit -json' returned no data."
echo "Ensure this is a valid Go module directory and 'go mod edit -json' works."
exit 1
fi
# --- Helper Functions using jq ---
# Function to extract direct dependencies
get_direct_dependencies() {
echo "$GO_MOD_JSON_DATA" | jq -r '.Require[]? | select(.Indirect != true) | .Path'
}
# Function to extract all dependencies (direct and indirect) with a given prefix
get_prefixed_dependencies() {
local prefix_to_match="$1"
echo "$GO_MOD_JSON_DATA" | jq -r --arg p_val "$prefix_to_match" '.Require[]? | select(.Path | startswith($p_val)) | .Path'
}
# --- Main Logic ---
echo "Go Module Updater (using go mod edit -json and jq)"
echo "-------------------------------------------------"
echo "Choose an action:"
echo "1. Update ALL direct dependencies"
echo "2. Update dependencies with a specific PREFIX (direct and indirect)"
echo "3. Cancel"
read -r -p "Enter your choice [1-3]: " choice
dependencies_to_update=""
case "$choice" in
1)
echo "Fetching direct dependencies..."
dependencies_to_update=$(get_direct_dependencies)
if [ -z "$dependencies_to_update" ]; then
echo "No direct dependencies found."
else
echo "The following direct dependencies will be targeted for update:"
echo "$dependencies_to_update"
fi
;;
2)
read -r -p "Enter the module prefix (e.g., github.com/poom/): " prefix_val
# Trim leading/trailing whitespace from the input prefix
prefix_val_trimmed=$(echo "$prefix_val" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
if [ -z "$prefix_val_trimmed" ]; then
echo "Error: Prefix cannot be empty after trimming. Original input: [$prefix_val]"
exit 1
fi
prefix_val="$prefix_val_trimmed"
echo "Fetching dependencies with prefix '$prefix_val'..."
dependencies_to_update=$(get_prefixed_dependencies "$prefix_val")
if [ -z "$dependencies_to_update" ]; then
echo "No dependencies found with the prefix '$prefix_val'."
else
echo "The following dependencies with prefix '$prefix_val' will be targeted for update:"
echo "$dependencies_to_update"
fi
;;
3)
echo "Operation cancelled."
exit 0
;;
*)
echo "Invalid choice. Exiting."
exit 1
;;
esac
if [ -z "$dependencies_to_update" ]; then
echo "Running 'go mod tidy' to ensure consistency..."
if go mod tidy; then
echo "'go mod tidy' completed successfully."
else
echo "Warning: 'go mod tidy' encountered errors."
fi
exit 0
fi
read -r -p "Proceed with updating the listed dependencies? (y/N): " confirm
if [[ "$confirm" != [yY] && "$confirm" != [yY][eE][sS] ]]; then
echo "Update cancelled by user."
exit 0
fi
echo ""
echo "Starting updates..."
# Loop through dependencies (handles multi-line string from jq output)
echo "$dependencies_to_update" | while IFS= read -r dep_path; do
if [ -n "$dep_path" ]; then # Ensure dep_path is not empty
echo "Updating $dep_path..."
if go get -u "$dep_path"; then
echo "$dep_path updated successfully."
else
echo "Warning: Failed to update $dep_path. Check for errors above."
fi
fi
done
echo ""
echo "All selected dependency updates attempted."
echo "Running 'go mod tidy' to clean up and finalize..."
if go mod tidy; then
echo "'go mod tidy' completed successfully."
else
echo "Warning: 'go mod tidy' encountered errors during 'go mod tidy'."
fi
echo "Script finished."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment