-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathparse-legacy-file
executable file
·62 lines (49 loc) · 1.75 KB
/
parse-legacy-file
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
#!/usr/bin/env bash
set -Eeuo pipefail
[[ -n ${DEBUG-} ]] && set -x
# override is exposed for testing purposes.
THIS_PLUGIN="${ASDF_HASHICORP_THIS_PLUGIN:-"$(basename "$(dirname "$(dirname "$0")")")"}"
SUPPORTED_PLUGINS=(terraform packer)
is_strict_equality_version_constraint() {
local -r version_constraint="$1"
grep --quiet -E '^=?[[:digit:]]+\.[[:digit:]]+.[[:digit:]]+' <<<"${version_constraint}"
}
read_version_from_main_tf() {
local -r version_file="$1"
local -r required_version_constraint="$(grep --no-filename \
--recursive \
--fixed-strings \
required_version \
"${version_file}" |
sed -E 's/[[:space:]]*required_version[[:space:]]*=//' |
tr -d '" ')"
[[ -z ${required_version_constraint} ]] && return 0
if is_strict_equality_version_constraint "${required_version_constraint}"; then
tr -d '=' <<<"${required_version_constraint}"
else
cat >&2 <<EOF
FATAL: Found legacy version file '${ASDF_HASHICORP_TERRAFORM_VERSION_FILE:-main.tf}' with unsupported required
version constraint expression: '${required_version_constraint}'. This
plugin only supports strict equality.
EOF
exit 1
fi
}
get_legacy_version() {
local -r version_file="$1"
local this_plugin_supported=false
# check if current plugin is supported
for plugin in "${SUPPORTED_PLUGINS[@]}"; do
if [[ ${plugin} == "${THIS_PLUGIN}" ]]; then
this_plugin_supported=true
break
fi
done
[[ ${this_plugin_supported} == false ]] && return 0
if [[ ${version_file} == *".${THIS_PLUGIN}-version" && -r ${version_file} ]]; then
cat "${version_file}"
elif [[ ${version_file} =~ ${ASDF_HASHICORP_TERRAFORM_VERSION_FILE:-main.tf} && -r ${version_file} ]]; then
read_version_from_main_tf "${version_file}"
fi
}
get_legacy_version "$1"