tfutils/tfenv

Auto install output to stdout can mix and mess with the terraform output

seprich opened this issue · 1 comments

The following shell scripting example can illustrate a problem, which can surprise the unsuspecting user:

ACTUAL_VERSION="$(terraform version -json | jq -r '.terraform_version')"

With vanilla terraform the above script will consistently run correctly (assuming terraform and jq are installed) producing the raw version number into the environment variable. However when using tfenv and having auto-install feature switched on with a .terraform-version file in the project structure then the above script will fail if tfenv needs to install the required version of terraform. Reason being that tfenv will output non-json text to the same stdout pipe as does terraform.

So I have 2 suggestions for improving the situation:

  1. There should be a configuration option to use for scripting which suppresses output from tfenv. e.g. SILENT_TFENV
  2. Currently the above problem can be avoided by executing tfenv install (version implicitly read from the .terraform-version file) before using any terraform commands in the scripts. This might be even preferable solution in most cases but it would be good to be documented in the README.

Third option: Whenever terraform gets the flag -json then the replacement script installed by tfenv recognizes that the overall output should respect the requested output format. As a result, either produce the output from the actual downloaded terraform only without decorations OR if adding extra output related to the installation then fit that output under extra json object property and output the combined json object.