Refactor check-runtime-versions script

Signed-off-by: Pratham Jagga <30550632+prathamjagga@users.noreply.github.com>
This commit is contained in:
Pratham Jagga 2024-10-13 18:41:59 +05:30 committed by GitHub
parent 0ccbacc303
commit fa9bb2d402
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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"