Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable terminal colors when not an interactive terminal #15264

Closed
mcandre opened this issue Jun 12, 2017 · 12 comments
Closed

Disable terminal colors when not an interactive terminal #15264

mcandre opened this issue Jun 12, 2017 · 12 comments

Comments

@mcandre
Copy link

mcandre commented Jun 12, 2017

When terraform is run by CI, or piped to less, or otherwise run in a non-tty environment, terminal codes should be disabled for more readable output.

@bclodius
Copy link
Contributor

@mcandre you can use -no-color argument. We found it very useful for our pipelines.

@jmgnc
Copy link

jmgnc commented Nov 23, 2017

This should be fixed.

A simple if (isatty(STDOUT_FILENO)) nocolorarg=1;

or the equivalent for the language is all that is needed.

@erikpaasonen
Copy link

Some Terraform commands are missing support for -no-color, e.g. terraform providers.

@apparentlymart apparentlymart added cli and removed core labels Apr 6, 2018
wking added a commit to wking/openshift-installer that referenced this issue Oct 5, 2018
We want to spit these out right away, instead of asking the caller to
re-run with --log-level=debug.

The -no-color additions work around [1]; without them stripping
newlines doesn't work, because there are color escapes in the way.

Inserting a bug to test:

  $ git diff -U1
  diff --git a/pkg/tfvars/tfvars.go b/pkg/tfvars/tfvars.go
  index d5f5543..8f44646 100644
  --- a/pkg/tfvars/tfvars.go
  +++ b/pkg/tfvars/tfvars.go
  @@ -19,3 +19,3 @@ type config struct {
   	 Name       string `json:"tectonic_cluster_name,omitempty"`
  -	 BaseDomain string `json:"tectonic_base_domain,omitempty"`
  +	 BaseDomain string `json:"xtectonic_base_domain,omitempty"`
   	 Masters    int    `json:"tectonic_master_count,omitempty"`
  $ hack/build.sh
  $ openshift-install cluster
  INFO Fetching OS image...
  INFO Using Terraform to create cluster...
  ERROR Failed to read tfstate ("/tmp/openshift-install-122374392/terraform.tfstate"): open /tmp/openshift-install-122374392/terraform.tfstate: no such file or directory
  ERROR Error: Error asking for user input: missing required value for "tectonic_base_domain"
  FATAL Failed to generate Cluster: failed to generate asset Cluster: failed to run terraform: failed to execute Terraform: exit status 1

[1]: hashicorp/terraform#15264
@dossy
Copy link

dossy commented Aug 27, 2020

terraform state show doesn't support -no-color either.

Would be great to be able to set TF_NO_COLOR=1 in the environment to disable all colorization completely, or something equally simple.

@Sasasu
Copy link

Sasasu commented Dec 21, 2020

image

Really need to add no-color to every command?
And keep answering confused people you need to add the no-color parameter?

terraform, are you serious?

@mcandre
Copy link
Author

mcandre commented Sep 8, 2021

cough

@bondido
Copy link

bondido commented Nov 9, 2021

You can use TF_CLI_ARGS environment variable, as advised here: #23708 (comment)

@kylegibson
Copy link

❯ terraform --version
Terraform v1.1.4
on linux_amd64

❯ TF_CLI_ARGS="-no-color" terraform state show | less
Exactly one argument expected.

Usage: terraform [global options] state show [options] ADDRESS

@pschulten
Copy link

pschulten commented May 11, 2022

terraform state show $(terraform state list|head -1) | sed -e 's/\x1b\[[0-9;]*m//g'

@ggustafsson
Copy link

I got bitten by this one recently and it was quite annoying so I went ahead and fixed it 🙂
PR #31253. No clue if it is good enough but we will see soon enough. Awaiting feedback.

@alisdair
Copy link
Contributor

We don't intend to automatically disable Terraform's terminal color output when not attached to an interactive terminal. There is software integrating with Terraform which redirects its output to a pipe or a file and expects the color UI output to remain, which means that we can't change this behavior for backwards compatibility reasons.

The -no-color flag should be supported by all commands which emit color codes, and as pointed out above this can be enabled by default using the TF_CLI_ARGS environment variable. This is our recommended approach for disabling color codes.

If you find a command which does not correctly support -no-color, please open a separate issue and we'll fix that bug. Thanks!

@alisdair alisdair closed this as not planned Won't fix, can't repro, duplicate, stale Jun 17, 2022
@github-actions
Copy link
Contributor

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests