terraform
terraform copied to clipboard
[cli] Emit shell completions to stdout
Terraform Version
Terraform v1.3.6
on linux_amd64
(using outdated nixpkgs stable branch)
Use Cases
- Easier packaging Nix expose completions from a special folder in referenced Nix components. This feature will allow basically a no brainer easy integration with how Nix works to generate completions. Instead of the user having to do an imperative step, the completions is available only by nix-shell terraform without a global installation.
Attempted Solutions
-
terraform -install-autocompleteImperative approach. Fails if you use home-manager because ~/.bashrc is a symlink to a file in the nix store, that is read only. -
HOME=/somewhere/else terraform -install-autocompleteTerraform ignores the environment variable and tries to write ~/.bashrc anyway.
Proposal
The same approach rclone uses to expose completions, ex:
rclone completion bashgenerate bash completion and output to stdout- It also works with fish, powershell and zsh, but support these shells is another problem.
References
- #28788
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
Thanks for the request!
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.
And mitchellh/cli hands off completion to posener/complete.
https://github.com/posener/complete/blob/9a4745ac49b29530e07dc2581745a218b646b7a3/cmd/install/bash.go#L6
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.
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.
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