package util import ( "strconv" "strings" "github.com/spf13/cobra" "golang.org/x/exp/slices" ) func ValidProjectsFunc(cmd *cobra.Command, args []string, toComplete string) ( []string, cobra.ShellCompDirective, ) { u := MustFromCtx(cmd.Context()) u.InitProjectCache(cmd, args) return u.Cache().ProjectStrings(toComplete), cobra.ShellCompDirectiveNoFileComp } func ValidAliasesFunc(cmd *cobra.Command, args []string, toComplete string) ( []string, cobra.ShellCompDirective, ) { utils := MustFromCtx(cmd.Context()) utils.InitProjectCache(cmd, args) return utils.Cache().AliasStrings(toComplete), cobra.ShellCompDirectiveNoFileComp } func (u *Utils) ValidProjectsOrAliasesFunc(cmd *cobra.Command, args []string, toComplete string) ( []string, cobra.ShellCompDirective, ) { projectStrings, _ := ValidAliasesFunc(cmd, args, toComplete) aliasStrings, _ := ValidProjectsFunc(cmd, args, toComplete) return append(projectStrings, aliasStrings...), cobra.ShellCompDirectiveDefault } func ValidRemotesFunc(cmd *cobra.Command, _ []string, toComplete string) ( []string, cobra.ShellCompDirective, ) { u := MustFromCtx(cmd.Context()) 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 }