tf-version-check 1.17 KB
Newer Older
John Jarvis's avatar
John Jarvis committed
1 2
#!/bin/bash
installed_version=$(terraform -version | cut -d v -f 2 | head -1)
3 4 5
env_tf_version_file="${dir:-.}/../environments/${environment:-.}/.terraform-version"
repo_tf_version_file="${dir:-.}/../.terraform-version"

John Jarvis's avatar
John Jarvis committed
6 7
if [[ -n $TF_VERSION ]]; then
    required_version="$TF_VERSION"
John Jarvis's avatar
John Jarvis committed
8 9
elif [[ -r "$env_tf_version_file" ]]; then
    required_version=$(cat "$env_tf_version_file")
10
elif [[ -r "$repo_tf_version_file" ]]; then
John Jarvis's avatar
John Jarvis committed
11
    required_version=$(cat "$repo_tf_version_file")
12 13 14
else
    echo -e "${_BRED}ERROR: ${_NORM}No suitable .terraform-version file found; exiting."
    exit 1
John Jarvis's avatar
John Jarvis committed
15
fi
John Jarvis's avatar
John Jarvis committed
16

John Jarvis's avatar
John Jarvis committed
17
if [[ "$installed_version" != "$required_version" ]]; then
18 19 20 21 22 23 24 25 26 27 28
    if [[ -x "$(which tfenv)" ]]; then
        echo -e "${_YELLOW}!!! NOTE !!!${_NORM}"
        echo "Setting terraform to use version ${required_version}."
        echo "Update the `.terraform-version` file(s) to use a different version"
        tfenv use "${required_version}"
    else
        echo -e "${_BRED}!!! WARNING !!!${_NORM}"
        echo "You are using the wrong version ($installed_version) of Terraform."
        echo "Please install version $required_version or proceed at your own risk, press enter to continue."
        read -rp ""
    fi
John Jarvis's avatar
John Jarvis committed
29
fi