|  | 
|  | 1 | +#!/bin/sh | 
|  | 2 | + | 
|  | 3 | +set -e | 
|  | 4 | + | 
|  | 5 | +# ensure controller-gen is at least the right version | 
|  | 6 | + | 
|  | 7 | + | 
|  | 8 | +download() { | 
|  | 9 | +	local version=$1 | 
|  | 10 | +	local P=$(pwd) | 
|  | 11 | +	local TMP_DIR=$(mktemp -d) | 
|  | 12 | +	cd $TMP_DIR | 
|  | 13 | +	go mod init tmp | 
|  | 14 | +	go get -u sigs.k8s.io/controller-tools/cmd/controller-gen@${version} | 
|  | 15 | +	cd ${P} | 
|  | 16 | + | 
|  | 17 | +	# clean up the temporary working directory | 
|  | 18 | +	rm -rf $TMP_DIR | 
|  | 19 | +} | 
|  | 20 | + | 
|  | 21 | +# see if the first semver argument is >= the second semver argument | 
|  | 22 | +later() { | 
|  | 23 | +	local compare=$1 | 
|  | 24 | +	local base=$2 | 
|  | 25 | +	# strip off the optional "v" at the beginning | 
|  | 26 | +	compare=${compare#v} | 
|  | 27 | +	base=${base#v} | 
|  | 28 | + | 
|  | 29 | +	local compareMajor=${compare%%.*} | 
|  | 30 | +	local baseMajor=${base%%.*} | 
|  | 31 | + | 
|  | 32 | +	local compareMinorPatch=${compare#*.} | 
|  | 33 | +	local baseMinorPatch=${base#*.} | 
|  | 34 | + | 
|  | 35 | +	local compareMinor=${compareMinorPatch%%.*} | 
|  | 36 | +	local baseMinor=${baseMinorPatch%%.*} | 
|  | 37 | + | 
|  | 38 | +	local comparePatch=${compare##*.} | 
|  | 39 | +	local basePatch=${base##*.} | 
|  | 40 | +	# check major version | 
|  | 41 | + | 
|  | 42 | +	# start with major - if greater, it is later; if less, it is earlier | 
|  | 43 | +	[ $compareMajor -lt $baseMajor ] && return 1 | 
|  | 44 | +	[ $compareMajor -gt $baseMajor ] && return 0 | 
|  | 45 | +	 | 
|  | 46 | +	# major matches, so check minor	 | 
|  | 47 | +	[ $compareMinor -lt $baseMinor ] && return 1 | 
|  | 48 | +	[ $compareMinor -gt $baseMinor ] && return 0 | 
|  | 49 | + | 
|  | 50 | +	# minor matches, so check patch | 
|  | 51 | +	[ $comparePatch -lt $basePatch ] && return 1 | 
|  | 52 | +	[ $comparePatch -gt $basePatch ] && return 0 | 
|  | 53 | + | 
|  | 54 | +	# patch matches, so it is the same | 
|  | 55 | +	return 0 | 
|  | 56 | +} | 
|  | 57 | + | 
|  | 58 | + | 
|  | 59 | +BINARY=$1 | 
|  | 60 | +VERSION=$2 | 
|  | 61 | + | 
|  | 62 | +# if no version given, just download the latest and go | 
|  | 63 | +if [ -z "$VERSION" ]; then | 
|  | 64 | +	download master | 
|  | 65 | +	exit 0 | 
|  | 66 | +fi | 
|  | 67 | + | 
|  | 68 | +# check if we have one and what its version is | 
|  | 69 | +if [ ! -e "${BINARY}" ]; then | 
|  | 70 | +	download ${VERSION} | 
|  | 71 | +	exit 0 | 
|  | 72 | +fi | 
|  | 73 | + | 
|  | 74 | +# if we got here, we have one, and we were not asked to take latest, so check its version | 
|  | 75 | +existing=$(${BINARY} --version | awk '{print $2}') | 
|  | 76 | +# get the three parts of the semver and the three parts of the requested version, and compare | 
|  | 77 | + | 
|  | 78 | +later "${existing}" "${VERSION}" || download ${VERSION} | 
|  | 79 | + | 
|  | 80 | +exit 0 | 
|  | 81 | +	 | 
|  | 82 | + | 
0 commit comments