Skip to content

Commit cb1bb68

Browse files
authored
refactor: use cobra.CompletionFunc type (#996)
Makes the code more readable by giving a keyword to the recurring type. (Same as #994, but using the type introduced in upstream Cobra)
1 parent 9bcfde7 commit cb1bb68

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

internal/cmd/cmpl/suggestions.go

+7-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// The returned function is mainly intended to be passed to
1414
// cobra/Command.RegisterFlagCompletionFunc or assigned to
1515
// cobra/Command.ValidArgsFunction.
16-
func SuggestCandidates(cs ...string) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
16+
func SuggestCandidates(cs ...string) cobra.CompletionFunc {
1717
return SuggestCandidatesF(func() []string {
1818
return cs
1919
})
@@ -23,9 +23,7 @@ func SuggestCandidates(cs ...string) func(*cobra.Command, []string, string) ([]s
2323
// to obtain a list of completion candidates. Once the list of candidates is
2424
// obtained the function returned by SuggestCandidatesF behaves like the
2525
// function returned by SuggestCandidates.
26-
func SuggestCandidatesF(
27-
cf func() []string,
28-
) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
26+
func SuggestCandidatesF(cf func() []string) cobra.CompletionFunc {
2927
return SuggestCandidatesCtx(func(*cobra.Command, []string) []string {
3028
return cf()
3129
})
@@ -37,10 +35,8 @@ func SuggestCandidatesF(
3735
// depend on a previously selected item like a server.
3836
//
3937
// Once the list of candidates is obtained the function returned by
40-
// SuggestCandidatesF behaves like the function returned by SuggestCandidates.
41-
func SuggestCandidatesCtx(
42-
cf func(*cobra.Command, []string) []string,
43-
) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
38+
// SuggestCandidatesCtx behaves like the function returned by SuggestCandidates.
39+
func SuggestCandidatesCtx(cf func(*cobra.Command, []string) []string) cobra.CompletionFunc {
4440
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
4541
cs := cf(cmd, args)
4642
if toComplete == "" {
@@ -60,7 +56,7 @@ func SuggestCandidatesCtx(
6056
}
6157

6258
// SuggestNothing returns a function that provides no suggestions.
63-
func SuggestNothing() func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
59+
func SuggestNothing() cobra.CompletionFunc {
6460
return func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
6561
return nil, cobra.ShellCompDirectiveDefault
6662
}
@@ -75,9 +71,7 @@ func SuggestNothing() func(*cobra.Command, []string, string) ([]string, cobra.Sh
7571
// calls the function at vfs[4] if it exists. To skip suggestions for an
7672
// argument in the middle of a list of arguments pass either nil or
7773
// SuggestNothing. Using SuggestNothing is preferred.
78-
func SuggestArgs(
79-
vfs ...func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective),
80-
) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
74+
func SuggestArgs(vfs ...cobra.CompletionFunc) cobra.CompletionFunc {
8175
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
8276
// Number of argument to suggest. args contains the already present
8377
// command line arguments.
@@ -95,8 +89,7 @@ func SuggestArgs(
9589

9690
// NoFileCompletion returns a function that provides completion suggestions without
9791
// file completion.
98-
func NoFileCompletion(f func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective)) func(
99-
*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
92+
func NoFileCompletion(f cobra.CompletionFunc) cobra.CompletionFunc {
10093
return func(command *cobra.Command, i []string, s string) ([]string, cobra.ShellCompDirective) {
10194
candidates, _ := f(command, i, s)
10295
return candidates, cobra.ShellCompDirectiveNoFileComp

internal/cmd/cmpl/suggestions_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestSuggestCandidates(t *testing.T) {
4444
func TestSuggestArgs(t *testing.T) {
4545
tests := []struct {
4646
name string
47-
vfs []func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective)
47+
vfs []cobra.CompletionFunc
4848
args []string
4949
sug []string
5050
d cobra.ShellCompDirective
@@ -58,14 +58,14 @@ func TestSuggestArgs(t *testing.T) {
5858
},
5959
{
6060
name: "suggest the only argument",
61-
vfs: []func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective){
61+
vfs: []cobra.CompletionFunc{
6262
cmpl.SuggestCandidates("aaaa"),
6363
},
6464
sug: []string{"aaaa"},
6565
},
6666
{
6767
name: "suggest the second of three possible arguments",
68-
vfs: []func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective){
68+
vfs: []cobra.CompletionFunc{
6969
cmpl.SuggestCandidates("aaaa"),
7070
cmpl.SuggestCandidates("bbbb"),
7171
cmpl.SuggestCandidates("cccc"),
@@ -75,7 +75,7 @@ func TestSuggestArgs(t *testing.T) {
7575
},
7676
{
7777
name: "skip suggestions using SuggestNothing",
78-
vfs: []func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective){
78+
vfs: []cobra.CompletionFunc{
7979
cmpl.SuggestCandidates("aaaa"),
8080
cmpl.SuggestNothing(),
8181
cmpl.SuggestCandidates("cccc"),
@@ -84,7 +84,7 @@ func TestSuggestArgs(t *testing.T) {
8484
},
8585
{
8686
name: "skip suggestions using nil",
87-
vfs: []func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective){
87+
vfs: []cobra.CompletionFunc{
8888
cmpl.SuggestCandidates("aaaa"),
8989
nil,
9090
cmpl.SuggestCandidates("cccc"),

0 commit comments

Comments
 (0)