This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
655425e
commit d46a565
Showing
3 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters