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

Add tab-completion support for PowerShell #29125

Closed
AjayKMehta opened this issue Jul 8, 2021 · 6 comments
Closed

Add tab-completion support for PowerShell #29125

AjayKMehta opened this issue Jul 8, 2021 · 6 comments
Labels
cli enhancement powershell Issues that seem to be specific to launching Terraform from PowerShell upstream

Comments

@AjayKMehta
Copy link

Current Terraform Version

1.0.0

Proposal

Please add tab-completion support for PowerShell to terraform -install-autocomplete.

@AjayKMehta AjayKMehta added enhancement new new issue not yet triaged labels Jul 8, 2021
@apparentlymart
Copy link
Member

Hi @AjayKMehta! Thanks for this feature request.

Terraform's tab-completion is powered by the external library github.com/posener/complete, so we support the same set of shells which that library supports. Therefore, if you send this request to that library or propose a PR that they accept then we can upgrade our version of that library to inherit that PowerShell support.

With that said, I should note that we don't currently recommend running Terraform with PowerShell on Windows because PowerShell's handling of quotes when running external programs (rather than PowerShell Cmdlets) is flawed, causing it to send incorrect command lines to Terraform in more complex situations such as input variables on the command line. You can use PowerShell with Terraform for simple commands, so we don't block it entirely, but we do recommend using Windows Command Prompt instead if you are on Windows, in order to avoid PowerShell corrupting the command line you've entered. For that reason, we're unlikely to prioritize implementing PowerShell autocomplete support ourselves, but we'd be happy to include it if the upstream library gains support for it.

(PowerShell on Linux and other Unix-like systems probably doesn't have this problem, because the Unix approach for passing arguments from a shell to a program is more compatible with PowerShell's design than that on Windows.)

Thanks!

@apparentlymart apparentlymart added cli powershell Issues that seem to be specific to launching Terraform from PowerShell upstream and removed new new issue not yet triaged labels Jul 9, 2021
@AjayKMehta
Copy link
Author

Thanks @apparentlymart for your informative reply. Yes, PowerShell's handling of of quotes when running external programs leaves much to be desired but it seems there is work being done on that front 🤞

A lot of the other CLI apps I use that are written in Go seem to use cobra which does have support for PowerShell 🙂

@apparentlymart
Copy link
Member

Hi @AjayKMehta,

Unfortunately Terraform was created before Cobra and so it uses other libraries for its CLI layer. We don't have any plans to rebuild it with Cobra, because that would be a lot of work and a lot of risk, with (by our estimation) very little benefit. While I would concede that moving to Cobra would potentially cause autocompletion to work on PowerShell, I'm skeptical that this benefit outweighs the potential cost.

On the plus side though, hopefully Cobra's example can illustrate how someone might add similar support to github.com/posener/complete, assuming that the upstream maintainers are interested in accepting such a change. Thanks for sharing it!

@AjayKMehta
Copy link
Author

AjayKMehta commented Jul 9, 2021

Understood. Thanks. I will create an issue in github.com/posener/complete for PowerShell support.

@hasancemcerit
Copy link

@AjayKMehta try this, it will give you some completion w/ tab. Although some of the subcommands that begin with single hypen (-) do not work. This must be related to the bug PowerShell/PowerShell#2912

# command completion for terraform cli
if(Get-Command terraform.exe -ErrorAction SilentlyContinue) {
    Register-ArgumentCompleter -Native -CommandName terraform -ScriptBlock {
        param($commandName, $wordToComplete, $cursorPosition)
            $env:COMP_LINE=$wordToComplete
            $env:COMP_POINT=$cursorPosition
            terraform.exe | ForEach-Object {
                [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
            }
            Remove-Item Env:\COMP_LINE
            Remove-Item Env:\COMP_POINT
    }
}

⬆️ @apparentlymart

@github-actions
Copy link

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 Dec 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cli enhancement powershell Issues that seem to be specific to launching Terraform from PowerShell upstream
Projects
None yet
Development

No branches or pull requests

3 participants