From fa9bb2d402ac3b13d8ffb73d43e6d3929d722f31 Mon Sep 17 00:00:00 2001 From: Pratham Jagga <30550632+prathamjagga@users.noreply.github.com> Date: Sun, 13 Oct 2024 18:41:59 +0530 Subject: [PATCH] Refactor check-runtime-versions script Signed-off-by: Pratham Jagga <30550632+prathamjagga@users.noreply.github.com> --- scripts/check-runtime-versions.sh | 50 ++++--------------------------- 1 file changed, 6 insertions(+), 44 deletions(-) diff --git a/scripts/check-runtime-versions.sh b/scripts/check-runtime-versions.sh index c1d6e6bca9..a1bf4045af 100755 --- a/scripts/check-runtime-versions.sh +++ b/scripts/check-runtime-versions.sh @@ -1,49 +1,11 @@ #!/usr/bin/env bash -# Required minimum versions. -REQUIRED_GO_VERSION=$(grep 'version:' .promu.yml | awk '{print $2}') -REQUIRED_NODE_VERSION=$(cat web/ui/.nvmrc | tr -d '\r' | sed 's/v//') +# Required versions for Nodejs and Go. +MIN_NODE_VERSION=$(cat web/ui/.nvmrc | tr -d '\r' | sed 's/v//') +MIN_GO_VERSION=$(awk '/^go / {print $2}' go.mod) -# Function to compare versions (checks if version A >= version B). -compare_versions() { - if [ "$1" = "$2" ]; then - return 0 - fi - local IFS=. - local i ver1=($1) ver2=($2) - for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do - ver1[i]=0 - done - for ((i=0; i<${#ver1[@]}; i++)); do - if [[ -z ${ver2[i]} ]]; then - ver2[i]=0 - fi - if ((10#${ver1[i]} > 10#${ver2[i]})); then - return 0 - fi - if ((10#${ver1[i]} < 10#${ver2[i]})); then - return 1 - fi - done - return 0 -} +# Check Nodejs version. +[ "$(echo -e "$(node --version)\n$MIN_NODE_VERSION" | sort -V | head -n 1)" = "$MIN_NODE_VERSION" ] && echo "Nodejs version OK" || echo "Warning: Installed Node.js version is less than the required version $MIN_NODE_VERSION" # Check Go version. -GO_VERSION=$(go version | awk '{print $3}' | sed 's/go//') -if compare_versions "$GO_VERSION" "$REQUIRED_GO_VERSION"; then - echo "Go version $GO_VERSION is OK" -else - echo "Go version $GO_VERSION is too old, required >= $REQUIRED_GO_VERSION" - exit 1 -fi - -# Check Node.js version. -NODE_VERSION=$(node -v | sed 's/v//') -if compare_versions "$NODE_VERSION" "$REQUIRED_NODE_VERSION"; then - echo "Node.js version $NODE_VERSION is OK" -else - echo "Node.js version $NODE_VERSION is too old, required >= $REQUIRED_NODE_VERSION" - exit 1 -fi - -echo "All versions are correct." +[ "$(echo -e "$(go version | awk '{print $3}')\n$MIN_GO_VERSION" | sort -V | head -n 1)" = "$MIN_GO_VERSION" ] && echo "Go version OK" || echo "Warning: Installed Go version is less than the required version $MIN_GO_VERSION"