Skip to content

Instantly share code, notes, and snippets.

@antonbabenko
Last active May 2, 2023 11:24
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonbabenko/675049186e54b770b4789886d2056639 to your computer and use it in GitHub Desktop.
Save antonbabenko/675049186e54b770b4789886d2056639 to your computer and use it in GitHub Desktop.
Make your terragrunt output useful
# 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
}
@antonbabenko
Copy link
Author

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 -- -

@dudicoco
Copy link

@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