terraform icon indicating copy to clipboard operation
terraform copied to clipboard

[cli] Emit shell completions to stdout

Open lucasew opened this issue 2 years ago • 7 comments

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-autocomplete Imperative 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-autocomplete Terraform ignores the environment variable and tries to write ~/.bashrc anyway.

Proposal

The same approach rclone uses to expose completions, ex:

  • rclone completion bash generate bash completion and output to stdout
  • It also works with fish, powershell and zsh, but support these shells is another problem.

References

  • #28788

lucasew avatar Feb 01 '23 00:02 lucasew

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

heitorPB avatar Feb 01 '23 00:02 heitorPB

Thanks for the request!

crw avatar Feb 01 '23 18:02 crw

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.

kmoe avatar Feb 02 '23 21:02 kmoe

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

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

lucasew avatar Feb 02 '23 21:02 lucasew

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.

heitorPB avatar Feb 03 '23 13:02 heitorPB

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.

rsteube avatar Apr 24 '23 13:04 rsteube

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

ghthor avatar Jun 27 '24 19:06 ghthor