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
}
@dudicoco
Copy link

@antonbabenko this doesn't seem to be working.

I'm currently adding the following to the end of the terragrunt commands: 2> >(grep -v "\[terragrunt]" >&2)

@antonbabenko
Copy link
Author

This still works well for me. Maybe we have different versions of tools installed? Also, I am using mac.

$ grep --version
grep (BSD grep) 2.5.1-FreeBSD
$ sed --version
sed (GNU sed) 4.8

Could you please show the complete command on line 5 which works for you?

@dudicoco
Copy link

dudicoco commented Sep 9, 2020

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

@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