From 6926e2c484e75bf17407f89f8200f5d18b34b84c Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 1 Mar 2023 10:47:52 -0800 Subject: [PATCH] `make` bails on the wrong go version This code: 1. Extracts the required go minor from `go.mod` 2. Gets the current go version from `go version` and normalizes it 3. Checks whether the required go minor is a version prefix for the current go version, and bails if it isn't. --- make.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/make.sh b/make.sh index 60a938426..7e5d3a61a 100755 --- a/make.sh +++ b/make.sh @@ -1,5 +1,19 @@ #!/bin/sh set -eu + +REQUIRED_GO_MINOR=$(sed -En 's/^go[[:space:]]+([[:digit:].]+)$/\1/p' go.mod) +ACTUAL_GO_VERSION=$(go version | sed -n 's/^go version go\([0-9]*\.[0-9]*\.[0-9]*\)\(.*\)/\1/p') + +# We use 'case' instead of '[' to match values against complex patterns, because POSIX +# does not guarantee that '[' supports advanced features like globs and regex. +if case "$ACTUAL_GO_VERSION" in "$REQUIRED_GO_MINOR".*) false ;; *) true ;; esac then + red="\e[0;91m" + reset="\e[0m" + + printf "${red}PROBLEM: You need go %s to build d2, but you have %s installed.${reset}\n" "$REQUIRED_GO_MINOR" "$ACTUAL_GO_VERSION" + exit 1 +fi + if [ ! -e "$(dirname "$0")/ci/sub/.git" ]; then set -x git submodule update --init