From 2374c7619263a4c50b42d9f721667c3a0a12682d Mon Sep 17 00:00:00 2001 From: Sander Declerck Date: Wed, 17 Dec 2025 09:35:10 +0100 Subject: [PATCH] Check current safe-chain version in installation script --- install-scripts/install-safe-chain.ps1 | 46 ++++++++++++++++++++++++++ install-scripts/install-safe-chain.sh | 38 +++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/install-scripts/install-safe-chain.ps1 b/install-scripts/install-safe-chain.ps1 index 9c0dcf7..16d2fc0 100644 --- a/install-scripts/install-safe-chain.ps1 +++ b/install-scripts/install-safe-chain.ps1 @@ -30,6 +30,46 @@ function Write-Error-Custom { 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 function Get-LatestVersion { try { @@ -114,6 +154,12 @@ function Install-SafeChain { $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 $installMsg = "Installing safe-chain $Version" if ($ci) { diff --git a/install-scripts/install-safe-chain.sh b/install-scripts/install-safe-chain.sh index 37d1710..54051c9 100755 --- a/install-scripts/install-safe-chain.sh +++ b/install-scripts/install-safe-chain.sh @@ -54,6 +54,38 @@ command_exists() { 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_version() { # Try using GitHub API to get the latest release tag @@ -155,6 +187,12 @@ main() { VERSION=$(fetch_latest_version) 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 INSTALL_MSG="Installing safe-chain ${VERSION}" if [ "$USE_CI_SETUP" = "true" ]; then