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 hidden or 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 | |
} |
I guess you are missing gnu-sed which you can get via brew install gsed
.
The default sed
on Mac really does not work:
$ /usr/bin/sed --version
/usr/bin/sed: illegal option -- -
@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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@antonbabenko sorry for the delay.
I'm using mac as well, no output for
sed --version
though.The command that works for me is not via
.bash_profile
, but added to the actual terragrunt command. For example:terragrunt plan-all 2> >(grep -v "\[terragrunt]" >&2)
I took this suggestion from one of the terragrunt github issues, though it would be nicer to use the
.bash_profile
solution.