git-project-manager/cmd/util/util_completion.go

75 lines
2.3 KiB
Go
Raw Normal View History

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