-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add an option
--tf-version
to terraform_docs, terraform_fmt, …
…and terraform_validate Enables the use of a version parameter which is in turn used by `tfenv` to dynamically switch the version of terraform being used at runtime.
- Loading branch information
Showing
6 changed files
with
144 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/sh | ||
function switchTfEnv() { | ||
TARGET_VERSION="$1" | ||
|
||
# Read current terraform version | ||
CURRENT_VERSION=$(tfenv list | sed -nr 's/^\* ([0-9\-\.]+) .*/\1/p') | ||
echo "Current version is set to '$CURRENT_VERSION', switching to '$TARGET_VERSION'" | ||
|
||
# Install and switch to required version | ||
tfenv install "$TARGET_VERSION" | ||
tfenv use "$TARGET_VERSION" | ||
|
||
# Auto-cleanup on exit | ||
trap restoreTfEnv EXIT | ||
} | ||
|
||
function restoreTfEnv() { | ||
# Restore | ||
echo "Restoring version to '$CURRENT_VERSION'" | ||
tfenv use "$CURRENT_VERSION" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,92 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
set -eo pipefail | ||
|
||
declare -a paths | ||
declare -a tfvars_files | ||
main() { | ||
initialize_ | ||
parse_cmdline_ "$@" | ||
|
||
index=0 | ||
# If a specific terraform version was specified, switch to it. The tfenv lib will auto-restore on exit | ||
[ "$TFVER" != "" ] && . "$_SCRIPT_DIR/lib_tfenv" && switchTfEnv "$TFVER" | ||
|
||
for file_with_path in "$@"; do | ||
file_with_path="${file_with_path// /__REPLACED__SPACE__}" | ||
terraform_fmt_ "${FILES[@]}" | ||
} | ||
|
||
paths[index]=$(dirname "$file_with_path") | ||
initialize_() { | ||
# get directory containing this script | ||
local dir | ||
local source | ||
source="${BASH_SOURCE[0]}" | ||
while [[ -L $source ]]; do # resolve $source until the file is no longer a symlink | ||
dir="$(cd -P "$(dirname "$source")" > /dev/null && pwd)" | ||
source="$(readlink "$source")" | ||
# if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located | ||
[[ $source != /* ]] && source="$dir/$source" | ||
done | ||
_SCRIPT_DIR="$(dirname "$source")" | ||
|
||
if [[ "$file_with_path" == *".tfvars" ]]; then | ||
tfvars_files+=("$file_with_path") | ||
fi | ||
# source getopt function | ||
# shellcheck source=lib_getopt | ||
. "$_SCRIPT_DIR/lib_getopt" | ||
} | ||
|
||
let "index+=1" | ||
done | ||
parse_cmdline_() { | ||
declare argv | ||
argv=$(getopt -o t: --long tf-version: -- "$@") || return | ||
eval "set -- $argv" | ||
|
||
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do | ||
path_uniq="${path_uniq//__REPLACED__SPACE__/ }" | ||
for argv; do | ||
case $argv in | ||
-t | --tf-version) | ||
shift | ||
TFVER="$1" | ||
shift | ||
;; | ||
--) | ||
shift | ||
FILES=("$@") | ||
break | ||
;; | ||
esac | ||
done | ||
} | ||
|
||
pushd "$path_uniq" > /dev/null | ||
terraform fmt | ||
popd > /dev/null | ||
done | ||
terraform_fmt_() { | ||
local -a -r files=("$@") | ||
declare -a paths | ||
declare -a tfvars_files | ||
|
||
# terraform.tfvars are excluded by `terraform fmt` | ||
for tfvars_file in "${tfvars_files[@]}"; do | ||
tfvars_file="${tfvars_file//__REPLACED__SPACE__/ }" | ||
index=0 | ||
|
||
terraform fmt "$tfvars_file" | ||
done | ||
for file_with_path in "${files[@]}"; do | ||
file_with_path="${file_with_path// /__REPLACED__SPACE__}" | ||
|
||
paths[index]=$(dirname "$file_with_path") | ||
|
||
if [[ "$file_with_path" == *".tfvars" ]]; then | ||
tfvars_files+=("$file_with_path") | ||
fi | ||
|
||
let "index+=1" | ||
done | ||
|
||
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do | ||
path_uniq="${path_uniq//__REPLACED__SPACE__/ }" | ||
|
||
pushd "$path_uniq" > /dev/null | ||
terraform fmt | ||
popd > /dev/null | ||
done | ||
|
||
# terraform.tfvars are excluded by `terraform fmt` | ||
for tfvars_file in "${tfvars_files[@]}"; do | ||
tfvars_file="${tfvars_file//__REPLACED__SPACE__/ }" | ||
|
||
terraform fmt "$tfvars_file" | ||
done | ||
} | ||
|
||
# global variables | ||
declare TFVER="" | ||
declare -a FILES=() | ||
|
||
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters