Skip to content

Commit 57fb38d

Browse files
authored
Fix #374: Route tfenv log output to stderr in terraform shim context (#469)
Fix #374: Route tfenv log output to stderr in terraform shim context
2 parents 0832b5c + bff4553 commit 57fb38d

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

bin/terraform

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ done;
5858
# Begin Script Body #
5959
#####################
6060

61+
# Signal to bashlog that we are in the terraform shim context.
62+
# All tfenv log output (info, warn) should go to stderr so it does
63+
# not mix with terraform's stdout and break piped commands like:
64+
# terraform version -json | jq
65+
export TFENV_SHIM_MODE=1;
66+
6167
log 'debug' "program=\"${0##*/}\"";
6268

6369
declare tfenv_path="${TFENV_ROOT}/bin/tfenv";

lib/bashlog.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ function log() {
136136
# Standard Output (Pretty)
137137
case "${level}" in
138138
'info'|'warn')
139-
echo -e "${std_line}";
139+
if [ "${TFENV_SHIM_MODE:-0}" -eq 1 ]; then
140+
# In shim mode (bin/terraform), all tfenv output goes to stderr
141+
# to avoid corrupting terraform's stdout (e.g. terraform version -json | jq)
142+
echo -e "${std_line}" >&2;
143+
else
144+
echo -e "${std_line}";
145+
fi;
140146
;;
141147
'debug')
142148
if [ "${debug_level}" -gt 0 ]; then

0 commit comments

Comments
 (0)