Package subcommand code
This commit is contained in:
71
cmd/util/util_completion.go
Normal file
71
cmd/util/util_completion.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
func (u *Utils) ValidProjectsFunc(cmd *cobra.Command, args []string, toComplete string) (
|
||||
[]string, cobra.ShellCompDirective,
|
||||
) {
|
||||
u.InitProjectCache(cmd, args)
|
||||
return u.Cache().ProjectStrings(toComplete), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
func (u *Utils) ValidAliasesFunc(cmd *cobra.Command, args []string, toComplete string) (
|
||||
[]string, cobra.ShellCompDirective,
|
||||
) {
|
||||
u.InitProjectCache(cmd, args)
|
||||
return u.Cache().AliasStrings(toComplete), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
func (u *Utils) ValidProjectsOrAliasesFunc(cmd *cobra.Command, args []string, toComplete string) (
|
||||
[]string, cobra.ShellCompDirective,
|
||||
) {
|
||||
projectStrings, _ := u.ValidAliasesFunc(cmd, args, toComplete)
|
||||
aliasStrings, _ := u.ValidProjectsFunc(cmd, args, toComplete)
|
||||
return append(projectStrings, aliasStrings...), cobra.ShellCompDirectiveDefault
|
||||
}
|
||||
|
||||
func (u *Utils) ValidRemotesFunc(_ *cobra.Command, _ []string, toComplete string) (
|
||||
[]string, cobra.ShellCompDirective,
|
||||
) {
|
||||
remotes := make([]string, 0, len(u.Config().Remotes))
|
||||
for _, remote := range u.Config().Remotes {
|
||||
if strings.HasPrefix(remote.Host, toComplete) {
|
||||
remotes = append(remotes, remote.Host)
|
||||
}
|
||||
}
|
||||
return remotes, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
func ValidLogLevelsFunc(_ *cobra.Command, _ []string, toComplete string) (
|
||||
[]string, cobra.ShellCompDirective,
|
||||
) {
|
||||
levels := []string{"info", "warn", "error", "debug"}
|
||||
matchingLevels := make([]string, 0, len(levels))
|
||||
for _, level := range levels {
|
||||
if strings.HasPrefix(level, toComplete) {
|
||||
matchingLevels = append(matchingLevels, level)
|
||||
}
|
||||
}
|
||||
return matchingLevels, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
func ValidProjectIdFunc(cmd *cobra.Command, args []string, toComplete string) (
|
||||
[]string, cobra.ShellCompDirective,
|
||||
) {
|
||||
u := MustFromCtx(cmd.Context())
|
||||
u.InitProjectCache(cmd, args)
|
||||
matchingIds := make([]string, 0, len(u.Cache().Projects))
|
||||
for _, p := range u.Cache().Projects {
|
||||
idString := strconv.FormatInt(int64(p.ID), 10)
|
||||
if strings.HasPrefix(idString, toComplete) {
|
||||
matchingIds = append(matchingIds, idString)
|
||||
}
|
||||
}
|
||||
return slices.Clip(matchingIds), cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
Reference in New Issue
Block a user