Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Add support for bash/zsh completion
Browse files Browse the repository at this point in the history
  • Loading branch information
yiannistri authored and jstevans committed Feb 18, 2020
1 parent 655425e commit d46a565
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
54 changes: 54 additions & 0 deletions cmd/fluxctl/completion_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"io"
"strings"

"github.com/spf13/cobra"
)

var (
completionShells = map[string]func(out io.Writer, cmd *cobra.Command) error{
"bash": runCompletionBash,
"zsh": runCompletionZsh,
}
)

func newCompletionCommand() *cobra.Command {
shells := []string{}
for s := range completionShells {
shells = append(shells, s)
}

return &cobra.Command{
Use: "completion SHELL",
DisableFlagsInUseLine: true,
Short: "Output shell completion code for the specified shell (bash or zsh)",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return newUsageError("please specify a shell")
}

if len(args) > 1 {
return newUsageError(fmt.Sprintf("please specify one of the following shells: %s", strings.Join(shells, " ")))
}

run, found := completionShells[args[0]]
if !found {
return newUsageError(fmt.Sprintf("unsupported shell type %q.", args[0]))
}

return run(cmd.OutOrStdout(), cmd.Parent())
},
ValidArgs: shells,
}
}

func runCompletionBash(out io.Writer, fluxctl *cobra.Command) error {
return fluxctl.GenBashCompletion(out)
}

func runCompletionZsh(out io.Writer, fluxctl *cobra.Command) error {
return fluxctl.GenZshCompletion(out)
}
39 changes: 39 additions & 0 deletions cmd/fluxctl/completion_cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"testing"
)

func TestCompletionCommand_InputFailure(t *testing.T) {
for k, v := range [][]string{
{},
{"foo"},
{"bash", "zsh"},
} {
t.Run(fmt.Sprintf("%d", k), func(t *testing.T) {
cmd := newCompletionCommand()
cmd.SetArgs(v)
if err := cmd.Execute(); err == nil {
t.Fatalf("Expecting error: command is expecting either bash or zsh")
}
})
}
}

func TestCompletionCommand_Success(t *testing.T) {
for k, v := range [][]string{
{"bash"},
{"zsh"},
} {
t.Run(fmt.Sprintf("%d", k), func(t *testing.T) {
parentCmd := newRoot().Command()
cmd := newCompletionCommand()
parentCmd.AddCommand(cmd)
cmd.SetArgs(v)
if err := cmd.Execute(); err != nil {
t.Fatalf("Expecting nil, got error (%s)", err.Error())
}
})
}
}
3 changes: 2 additions & 1 deletion cmd/fluxctl/root_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (opts *rootOpts) Command() *cobra.Command {
newIdentity(opts).Command(),
newSync(opts).Command(),
newInstall().Command(),
newCompletionCommand(),
)

return cmd
Expand All @@ -105,7 +106,7 @@ func (opts *rootOpts) Command() *cobra.Command {
func (opts *rootOpts) PersistentPreRunE(cmd *cobra.Command, _ []string) error {
// skip port forward for certain commands
switch cmd.Use {
case "version":
case "version", "completion SHELL":
fallthrough
case "install":
return nil
Expand Down

0 comments on commit d46a565

Please sign in to comment.