Last active
May 2, 2023 11:24
-
-
Save antonbabenko/675049186e54b770b4789886d2056639 to your computer and use it in GitHub Desktop.
Make your terragrunt output useful
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
# Put this function in your ~/.bash_profile or similar and use `terragrunt` as before. | |
# From: https://github.com/gruntwork-io/bash-commons/blob/master/modules/bash-commons/src/array.sh | |
# Returns 0 if the given item (needle) is in the given array (haystack); returns 1 otherwise. | |
array_contains() { | |
local -r needle="$1" | |
shift | |
local -ra haystack=("$@") | |
local item | |
for item in "${haystack[@]}"; do | |
if [[ "$item" == "$needle" ]]; then | |
return 0 | |
fi | |
done | |
return 1 | |
} | |
terragrunt() { | |
local action=$1 | |
shift 1 | |
# For older version of Terragrunt | |
# command terragrunt $action "$@" 2>&1 | sed -E "s|$(dirname $(pwd))/||g;s|^\[terragrunt\]( [0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})* ||g;s|(\[.*\]) [0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}|\1|g" | |
# Notes: | |
# 2>&1 - may not work when input is expected from users | |
# GNU versions of xargs, realpath is used | |
if array_contains "$action" "plan" "apply" "destroy" "run-all" "plan-all" "apply-all" "destroy-all"; then | |
command terragrunt $action -lock=false -compact-warnings --terragrunt-log-level error --terragrunt-include-module-prefix --terragrunt-use-partial-parse-config-cache --terragrunt-include-external-dependencies "$@" 2>&1 | sed -E "s|^\[([^ ]+)\](.*)|\1#\2|" | gxargs -I '{}' sh -c 'input="{}"; IFS="#" read -r path rest <<< "$input"; printf "[%s]%s\n" $(grealpath --zero --relative-to=$(pwd) "$path") "$rest";' | |
else | |
# Default: nothing special | |
command terragrunt $action --terragrunt-include-module-prefix --terragrunt-use-partial-parse-config-cache "$@" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@antonbabenko i've installed gsed but it's still not working, and the solution which i've been using doesn't work anymore as well.
Looks like the Terragrunt output was changed in one of the latest versions, and now instead of
[terragrunt]
the output contains[path/to/dir]
, so the grep/sed doesn't work anymore.