Check current safe-chain version in installation script

This commit is contained in:
Sander Declerck 2025-12-17 09:35:10 +01:00
parent 6bb0cedf21
commit 2374c76192
No known key found for this signature in database
2 changed files with 84 additions and 0 deletions

View file

@ -30,6 +30,46 @@ function Write-Error-Custom {
exit 1 exit 1
} }
# Get currently installed version of safe-chain
function Get-InstalledVersion {
# Check if safe-chain command exists
if (-not (Get-Command safe-chain -ErrorAction SilentlyContinue)) {
return $null
}
try {
# Execute safe-chain -v and capture output
$output = & safe-chain -v 2>&1
# Extract version from "Current safe-chain version: X.Y.Z" output
if ($output -match "Current safe-chain version:\s*(.+)") {
return $matches[1].Trim()
}
return $null
}
catch {
return $null
}
}
# Check if the requested version is already installed
function Test-VersionInstalled {
param([string]$RequestedVersion)
$installedVersion = Get-InstalledVersion
if ([string]::IsNullOrWhiteSpace($installedVersion)) {
return $false
}
# Strip leading 'v' from versions if present for comparison
$requestedClean = $RequestedVersion -replace '^v', ''
$installedClean = $installedVersion -replace '^v', ''
return $requestedClean -eq $installedClean
}
# Fetch latest release version tag from GitHub # Fetch latest release version tag from GitHub
function Get-LatestVersion { function Get-LatestVersion {
try { try {
@ -114,6 +154,12 @@ function Install-SafeChain {
$Version = Get-LatestVersion $Version = Get-LatestVersion
} }
# Check if the requested version is already installed
if (Test-VersionInstalled -RequestedVersion $Version) {
Write-Info "safe-chain $Version is already installed"
exit 0
}
# Build installation message # Build installation message
$installMsg = "Installing safe-chain $Version" $installMsg = "Installing safe-chain $Version"
if ($ci) { if ($ci) {

View file

@ -54,6 +54,38 @@ command_exists() {
command -v "$1" >/dev/null 2>&1 command -v "$1" >/dev/null 2>&1
} }
# Get currently installed version of safe-chain
get_installed_version() {
if ! command_exists safe-chain; then
echo ""
return
fi
# Extract version from "Current safe-chain version: X.Y.Z" output
installed_version=$(safe-chain -v 2>/dev/null | grep "Current safe-chain version:" | sed -E 's/.*: (.*)/\1/')
echo "$installed_version"
}
# Check if the requested version is already installed
is_version_installed() {
requested_version="$1"
installed_version=$(get_installed_version)
if [ -z "$installed_version" ]; then
return 1 # Not installed
fi
# Strip leading 'v' from versions if present for comparison
requested_clean=$(echo "$requested_version" | sed 's/^v//')
installed_clean=$(echo "$installed_version" | sed 's/^v//')
if [ "$requested_clean" = "$installed_clean" ]; then
return 0 # Same version installed
else
return 1 # Different version installed
fi
}
# Fetch latest release version tag from GitHub # Fetch latest release version tag from GitHub
fetch_latest_version() { fetch_latest_version() {
# Try using GitHub API to get the latest release tag # Try using GitHub API to get the latest release tag
@ -155,6 +187,12 @@ main() {
VERSION=$(fetch_latest_version) VERSION=$(fetch_latest_version)
fi fi
# Check if the requested version is already installed
if is_version_installed "$VERSION"; then
info "safe-chain ${VERSION} is already installed"
exit 0
fi
# Build installation message # Build installation message
INSTALL_MSG="Installing safe-chain ${VERSION}" INSTALL_MSG="Installing safe-chain ${VERSION}"
if [ "$USE_CI_SETUP" = "true" ]; then if [ "$USE_CI_SETUP" = "true" ]; then