-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
19 changed files
with
265 additions
and
148 deletions.
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
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
This file was deleted.
Oops, something went wrong.
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,80 @@ | ||
package actions | ||
|
||
import ( | ||
"slices" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type completionFunc func( | ||
cmd *cobra.Command, | ||
args []string, | ||
toComplete string, | ||
) ([]string, cobra.ShellCompDirective) | ||
|
||
func completePayloadName(acts *Actions) completionFunc { | ||
return func( | ||
cmd *cobra.Command, | ||
_ []string, | ||
toComplete string, | ||
) ([]string, cobra.ShellCompDirective) { | ||
payloads, err := (*acts).PayloadsList(cmd.Context(), PayloadsListParams{ | ||
Name: toComplete, | ||
}) | ||
if err != nil { | ||
return nil, cobra.ShellCompDirectiveError | ||
} | ||
|
||
names := make([]string, len(payloads)) | ||
|
||
for i, p := range payloads { | ||
names[i] = p.Name | ||
} | ||
|
||
return names, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
} | ||
|
||
func completeOne(list []string) completionFunc { | ||
return func( | ||
_ *cobra.Command, | ||
_ []string, | ||
_ string, | ||
) ([]string, cobra.ShellCompDirective) { | ||
return list, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
} | ||
|
||
func completeMany(completions []string) completionFunc { | ||
return func( | ||
_ *cobra.Command, | ||
_ []string, | ||
toComplete string, | ||
) ([]string, cobra.ShellCompDirective) { | ||
// aaa,bbb,c -> [aaa, bbb, c] | ||
parts := strings.Split(toComplete, ",") | ||
|
||
// [aaa, bb, c] -> prefix = "aaa,bbb," | ||
prefix := strings.Join(parts[:len(parts)-1], ",") | ||
if prefix != "" { | ||
prefix += "," | ||
} | ||
|
||
// lastPart = "c" | ||
lastPart := parts[len(parts)-1] | ||
|
||
// Filter completions based on the current input | ||
var result []string | ||
for _, comp := range completions { | ||
if slices.Contains(parts, comp) { | ||
continue | ||
} | ||
if strings.HasPrefix(comp, lastPart) { | ||
result = append(result, prefix+comp) | ||
} | ||
} | ||
|
||
return result, cobra.ShellCompDirectiveNoSpace | ||
} | ||
} |
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
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
Oops, something went wrong.