Skip to content

Commit

Permalink
Minimal shell completion support (argoproj#807)
Browse files Browse the repository at this point in the history
* Minimal shell completion support.

The subcommand follows the pattern of kubectl's completion support,
though that has considerable extensions.

zsh support in cobra is minimal at the moment and doesn't support flag
completion. However, there are open PRs against it, so it doesn't seem
worth going the Kubernetes route which modifies the bash output to make
it suitable for zsh.

An obvious next step would be completing to workflow names. I doubt this
would be as useful as pod completions in kubectl, as there isn't often
a useful prefix (if you run many of the same type of workflow).

* Tweak language.

* Hopefully fix lint error.

I've restructured the flow so there's only one point to check the error.
  • Loading branch information
mthx authored and alexmt committed Mar 27, 2018
1 parent c83ad24 commit 37680ef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions cmd/argo/commands/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package commands

import (
"fmt"
"io"
"log"
"os"

"github.com/spf13/cobra"
)

func NewCompletionCommand() *cobra.Command {
var command = &cobra.Command{
Use: "completion SHELL",
Short: "output shell completion code for the specified shell (bash or zsh)",
Long: `Write bash or zsh shell completion code to standard output.
For bash, ensure you have bash completions installed and enabled.
To access completions in your current shell, run
$ source <(argo completion bash)
Alternatively, write it to a file and source in .bash_profile
For zsh, output to a file in a directory referenced by the $fpath shell
variable.
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
shell := args[0]
rootCommand := NewCommand()
availableCompletions := map[string]func(io.Writer) error{
"bash": rootCommand.GenBashCompletion,
"zsh": rootCommand.GenZshCompletion,
}
completion, ok := availableCompletions[shell]
if !ok {
fmt.Printf("Invalid shell '%s'. The supported shells are bash and zsh.\n", shell)
os.Exit(1)
}
if err := completion(os.Stdout); err != nil {
log.Fatal(err)
}
},
}

return command
}
1 change: 1 addition & 0 deletions cmd/argo/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewCommand() *cobra.Command {
},
}

command.AddCommand(NewCompletionCommand())
command.AddCommand(NewDeleteCommand())
command.AddCommand(NewGetCommand())
command.AddCommand(NewInstallCommand())
Expand Down

0 comments on commit 37680ef

Please sign in to comment.