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
42.64k stars 9.55k forks source link

[cli] Emit shell completions to stdout #32611

Open lucasew opened 1 year ago

lucasew commented 1 year ago

Terraform Version

Terraform v1.3.6
on linux_amd64

(using outdated nixpkgs stable branch)

Use Cases

Attempted Solutions

Proposal

The same approach rclone uses to expose completions, ex:

References

heitorPB commented 1 year ago

This is what I get when I try to install the completions in NixOs (this should be done when installing Terraform via package manager, not by the user IMO):

$ terraform -install-autocomplete
Error executing CLI: 1 error occurred:
        * open /home/h/.bashrc: permission denied
crw commented 1 year ago

Thanks for the request!

kmoe commented 1 year ago

Autocompletion is handed off entirely to mitchellh/cli. The proposal makes sense to me, though we'd likely want to introduce it in a backwards-compatible way with an additional arg of some sort.

I suggest opening an issue at https://github.com/mitchellh/cli/issues - Mitchell is a Nix user so I'm sure he'll be sympathetic.

lucasew commented 1 year ago

And mitchellh/cli hands off completion to posener/complete.

https://github.com/posener/complete/blob/9a4745ac49b29530e07dc2581745a218b646b7a3/cmd/install/bash.go#L6

heitorPB commented 1 year ago

And posener/complete hardcodes where it writes the scripts. It would be handy to be able to write to stdout, so we can redirect to any file we want.

rsteube commented 1 year ago

The completion scripts only set COMP_LINE and invoke the command so they normally don't change between versions. Makes it pretty simple to bridge for other shells as well. For now you could just generate them once and add them to the package.

ghthor commented 4 months ago

While I think this would be nice, as this seems to be a general pattern where the command can generate the completion scripts for each shell (ala cobra), the completion script is so minimal for the hashicorp CLI tools because of this comments (https://github.com/hashicorp/terraform/issues/32611#issuecomment-1520192253) statement.

Nix package just generates this scripts line.

https://github.com/ghthor/nixpkgs/blob/a71e967ef3694799d0c418c98332f7ff4cc5f6af/pkgs/applications/networking/cluster/terraform/default.nix#L41