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