hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
41.76k stars 9.42k forks source link

Requesting machine-enumaratable workspace listing #34775

Open LeBaronDeCharlus opened 4 months ago

LeBaronDeCharlus commented 4 months ago

Terraform Version

Terraform v1.6.0
on linux_amd64

Your version of Terraform is out of date! The latest version
is 1.7.4. You can update by downloading from https://www.terraform.io/downloads.html

Use Cases

ENHANCEMENTS

This PR proposition updates the visual indicator for the current workspace in the workspace list command output.

The asterisk (*) has been replaced with a greater-than symbol (>) to denote the active workspace and prevent unwanted behavior from wildcard symbol.

This change is reflected in both the workspace command tests and the workspace list command implementation.

Current

When you call workspace list argument, you have an output with your current workspace selected with * symbol like this :

$ terraform workspace list
  * default

If you want to iterate over the output you will have an issue because of the wildcard.

$ for i in $(terraform workspace list) ; do echo -ne "$i" ; done
default main.tf api.tf modules README.md terraform.tfvars.example test.tfvars variables.tf default test

We can see here that during the iteration, the for made a echo * because of the wildcard and print the files in the current directory.

Attempted Solutions

Bypassing

To make it work, we have to add a sed command to remove the character.

for i in $(terraform workspace list | sed 's/*//g'); do echo "$i"; done
default

Proposal

Proposition

Replace * by > (or whatever character that is not a wildcard) so we can simply do :

for i in $(terraform workspace list) ; do echo -ne "$i" ; done
>
default

Now even if > char is present, we do not have unwanted behavior with wildcard substitution when piping.

That would be much more easier for shell programming (for eg) to parse output without bad surprise.

References

No response

crw commented 4 months ago

Thanks for your interest in this issue! This is just a reminder to please avoid "+1" comments, and to use the upvote mechanism (click or add the 👍 emoji to the original post) to indicate your support for this issue. This helps avoid notification spam for issues with high numbers of participants while enabling the maintainers to prioritize issues. Thanks again for the feedback!