Skip to content

Proposal: Auto-completion in Go instead of bash #7321

Closed

Description

@marckhouzam

The cobra project provides ways for Helm to perform custom auto-completions (e.g., helm status <TAB> or helm status --namespace <TAB>) (see Cobra's bash_completions.md for details).

The problem is that these methods require Helm to inject shell scripts into the global auto-completion script. So Helm developers have to code these completions in bash instead of in Go. Furthermore, debugging these completions is very tedious. Maintaining them is also complex. There are currently over 250 lines of bash script injected by Helm. See the code starting at

bashCompletionFunc = `

This proposal aims for shell auto-completion code to be written in Go instead of bash.
In summary, Helm would support a new hidden command helm __complete, which the Cobra auto-completion script would call to obtain the auto-completion choices.

The current completion flow looks as follows.
Please open on GitHub to see diagram properly

                     bash completion script
               ____________________________________  helm list -aqm 1000  ______
 ______       |  ___________        _____________  |    ----------->     |      |
|      |      | | generated |      |    helm     | |                     |      |
| User | ---> | |    by     |      |   custom    | |   helm repo list    | helm |
|      |      | |   Cobra   | ---> | completions | |    ----------->     | bin  |
 ------       | |   (bash)  |      |   (bash)    | |                     | (Go) |
              |  -----------        -------------  |  helm plugin list   |      |
               ------------------------------------     ----------->      ------
helm status <TAB>                         ^                                 |
helm repo remove <TAB>                    |                                 |
helm plugin update <TAB>                   ---------------------------------
        etc                                 output must be parsed by script
                                                       (bash)

The new prosed completion flow would look like this:

                bash completion script
              __________________________     helm __complete status      _____________
 ______      | ___________ ____________ |       --------------->        |   helm bin  |
|      |     || generated |  generic   ||                               | __complete  |
| User | --> ||    by     | use of cmd ||  helm __complete repo remove  |   cmd and   |
|      |     ||   Cobra   | __complete ||       --------------->        |  completion |
 ------      ||   (bash)  |   (bash)   ||                               |   routines  |
             | ----------- ------------ | helm __complete plugin update |     (Go)    |
              --------------------------        --------------->         -------------
helm status <TAB>                                      ^^^
helm repo remove <TAB>                      generic request for helm           
helm plugin update <TAB>                   to complete the command-line
        etc

Advantages of this approach:

  • Custom completions written in Go instead of bash
  • Access to full Helm code base to perform completions instead of having to rely on calling helm externally. For example, calling helm search repo is more limited than coding the logic directly in helm in Go.
  • Debugging and development much easier because helm developers can directly call e.g., helm __complete status j<ENTER> to test the corresponding completion code
  • The Go debugger can even be used
  • Go tests can be written for the custom completion code
  • Flag parsing done automatically by Cobra (instead of having to be coded in bash by helm)
  • Global flags handled automatically instead of being coded explicitly in bash (see changes required by feat(cli) Pass token via cli #7230)
  • No need to make sure the custom completion code works for both bash and zsh
  • No more issues with the use of shell tools (e.g., echo, cat, tail) that work differently on different OSes (e.g., bash completion: tail: cannot open ‘+2’ for reading: No such file or directory #7304)
  • Use of client-go library instead of kubectl

Activity

  1. marckhouzam commented on Jan 1, 2020

    @marckhouzam
    MemberAuthor

    Ultimately I'd like to push this down to Cobra. However to get the benefits faster, I suggest starting in Helm. I will be posting a PR very soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions