|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# generated with Cursor and claude-3.5-sonnet |
| 4 | + |
| 5 | +# This script checks for version updates of tools defined in the Dockerfile. |
| 6 | +# It expects ARG lines with VERSION in their name, and corresponding GitHub URLs. |
| 7 | +# The script extracts current versions from the Dockerfile, fetches the latest |
| 8 | +# versions from GitHub, and compares them. It then displays both versions, |
| 9 | +# highlighting any differences. Use --update to update the Dockerfile. |
| 10 | +# NOTE: only tested on macOS versions of grep, sed, curl, and awk. |
| 11 | + |
| 12 | + |
| 13 | +# Text formatting |
| 14 | +BOLD='\033[1m' |
| 15 | +RESET='\033[0m' |
| 16 | + |
| 17 | +# Optionally support GitHub API token for rate limiting via shell variable |
| 18 | +GITHUB_API_KEY="${GITHUB_API_KEY:-}" |
| 19 | + |
| 20 | +# Function to extract version from GitHub API |
| 21 | +get_latest_version() { |
| 22 | + local repo="$1" |
| 23 | + local api_url="https://api.github.com/repos/${repo}/releases/latest" |
| 24 | + local curl_opts=(-s) |
| 25 | + |
| 26 | + # Add authorization header if API key is provided |
| 27 | + if [ -n "$GITHUB_API_KEY" ]; then |
| 28 | + curl_opts+=(-H "Authorization: token $GITHUB_API_KEY") |
| 29 | + fi |
| 30 | + |
| 31 | + # Fetch the latest version and extract the tag name |
| 32 | + local version=$(curl "${curl_opts[@]}" "$api_url" | jq -r .tag_name) |
| 33 | + |
| 34 | + # Strip non-numerical characters, keeping only numbers and dots |
| 35 | + version=$(echo "$version" | sed 's/[^0-9.]//g') |
| 36 | + echo "$version" |
| 37 | +} |
| 38 | + |
| 39 | +# Detect if running on macOS |
| 40 | +is_macos() { |
| 41 | + [[ "$(uname -s)" == "Darwin" ]] |
| 42 | +} |
| 43 | + |
| 44 | +# Function to update Dockerfile |
| 45 | +update_dockerfile() { |
| 46 | + local package="$1" |
| 47 | + local new_version="$2" |
| 48 | + if is_macos; then |
| 49 | + # macOS version of sed requires an empty string after -i |
| 50 | + sed -i '' "s/ARG ${package}_VERSION=.*/ARG ${package}_VERSION=${new_version}/" Dockerfile |
| 51 | + else |
| 52 | + # Linux version of sed |
| 53 | + sed -i "s/ARG ${package}_VERSION=.*/ARG ${package}_VERSION=${new_version}/" Dockerfile |
| 54 | + fi |
| 55 | +} |
| 56 | + |
| 57 | +# Check for CLI option |
| 58 | +update_mode=false |
| 59 | +if [ "$1" == "--update" ]; then |
| 60 | + update_mode=true |
| 61 | +fi |
| 62 | + |
| 63 | +# Read the Dockerfile |
| 64 | +dockerfile_content=$(cat Dockerfile) |
| 65 | + |
| 66 | +# Find all ARGs with VERSION in their name |
| 67 | +while IFS= read -r line; do |
| 68 | + if [[ $line =~ ARG.*VERSION ]]; then |
| 69 | + # Extract the package name and current version |
| 70 | + arg_name=$(echo "$line" | awk -F'=' '{print $1}' | awk '{print $2}') |
| 71 | + current_version=$(echo "$line" | awk -F'=' '{print $2}') |
| 72 | + |
| 73 | + # Find the corresponding GitHub URL |
| 74 | + github_url=$(echo "$dockerfile_content" | grep -B 5 "$line" | grep 'https://github.com' | tail -n1 | awk '{print $2}') |
| 75 | + |
| 76 | + if [[ -n $github_url ]]; then |
| 77 | + # Extract owner and repo from GitHub URL |
| 78 | + repo=$(echo "$github_url" | sed -E 's|https://github.com/||' | sed -E 's|/releases/latest||') |
| 79 | + |
| 80 | + # Get the latest version from GitHub |
| 81 | + latest_version=$(get_latest_version "$repo") |
| 82 | + |
| 83 | + # Display version information |
| 84 | + echo "Package: ${arg_name%_VERSION}" |
| 85 | + echo " Dockerfile version: $current_version" |
| 86 | + if [ "$current_version" != "$latest_version" ]; then |
| 87 | + echo -e " Latest version: ${BOLD}${latest_version}${RESET}" |
| 88 | + if [ "$update_mode" = true ]; then |
| 89 | + # Prompt user for update |
| 90 | + echo -n "Do you want to update this package to the latest version? (y/N) " |
| 91 | + read -r response < /dev/tty |
| 92 | + if [[ "$response" =~ ^[Yy]$ ]]; then |
| 93 | + update_dockerfile "${arg_name%_VERSION}" "$latest_version" |
| 94 | + echo "Updated ${arg_name%_VERSION} to version $latest_version in Dockerfile" |
| 95 | + fi |
| 96 | + fi |
| 97 | + else |
| 98 | + echo " Latest version: $latest_version" |
| 99 | + fi |
| 100 | + echo |
| 101 | + fi |
| 102 | + fi |
| 103 | +done < <(echo "$dockerfile_content") |
| 104 | + |
| 105 | +if [ "$update_mode" = false ]; then |
| 106 | + echo "Run with --update option to enable updating the Dockerfile." |
| 107 | +fi |
0 commit comments