-
Notifications
You must be signed in to change notification settings - Fork 370
/
vars.sh
executable file
·81 lines (66 loc) · 1.43 KB
/
vars.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env sh
#shellcheck disable=SC3043
set -eu
# Don't deal with recursive make stuff here!
unset -v MAKEFLAGS MAKELEVEL
from=
var=
print_usage() {
echo 'Query Makefile variables from scripts. Use like so:'
echo
echo " $0 go_version"
echo " $0 FROM=docs python_version"
}
version_from_go_mod() {
local pkg version rest
while read -r pkg version rest; do
if [ "$pkg" = "$1" ]; then
printf %s "$version"
return 0
fi
done
return 1
}
fail() {
echo "$@" >&2
print_usage >&2
exit 1
}
[ $# -gt 0 ] || { print_usage && exit; }
while [ $# -gt 0 ]; do
case "$1" in
*=*)
name="${1%%=*}" && val="${1#*=}"
[ "$name" = FROM ] || fail Unsupported variable "$name"
[ -z "$from" ] || fail FROM already given
[ -n "$val" ] || fail FROM may not be empty
from="$val"
;;
*)
[ -z "$var" ] || fail Makefile variable already given
var="$1"
;;
esac
shift 1
done
[ -n "$var" ] || fail Makefile variable not given
[ -n "$from" ] || from=embedded-bins
case "$var" in
k0sctl_version) pkg=github.com/k0sproject/k0sctl ;;
k0s_sort_version) pkg=github.com/k0sproject/version ;;
*) pkg='' ;;
esac
if [ -n "$pkg" ]; then
version_from_go_mod "$pkg" <"$from"/go.mod
exit 0
fi
exec make --no-print-directory -r -s -f - <<EOF
include $from/Makefile.variables
.PHONY: print
print:
ifeq (\$(origin $var),file)
\$(info \$($var))
else
\$(error Makefile variable $var doesn't exist)
endif
EOF