From 86d55197cb2818382747d6facf197f14a28a2d01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Sat, 2 Jul 2022 12:05:12 +0200 Subject: [PATCH] Add semver shell helpers --- zsh/zshrc.d/40_functions.sh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/zsh/zshrc.d/40_functions.sh b/zsh/zshrc.d/40_functions.sh index ac552d9..f6fcc90 100644 --- a/zsh/zshrc.d/40_functions.sh +++ b/zsh/zshrc.d/40_functions.sh @@ -214,3 +214,33 @@ kubectl_pod() { kubectl_deployment() { kubectl-env mycloud get -n "${1}" deployment --selector=${2} -o jsonpath='{.items[*].metadata.name}' } + +# The semver_ checks are inspired by +# https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash +semver_lte() { + v1="${1}" + v2="${2}" + + printf '%s\n%s' "${v1}" "${v2}" | sort --version-sort --check=silent +} + +semver_lt() { + v1="${1}" + v2="${2}" + + semver_lte "${v1}" "${v2}" && [[ ! "${v1}" == "${v2}" ]] +} + +semver_gte() { + v1="${1}" + v2="${2}" + + ! semver_lt "${v1}" "${v2}" +} + +semver_gt() { + v1="${1}" + v2="${2}" + + ! semver_lte "${v1}" "${v2}" +}