Add facet completion func

This commit is contained in:
2024-11-26 12:16:27 -05:00
parent 42bf0696d6
commit dbe71e2063
5 changed files with 53 additions and 12 deletions

View File

@ -9,7 +9,36 @@ import (
"gitea.libretechconsulting.com/50W/eia-api-go/pkg/eia"
)
func CompleteRoutes(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Given args of <route> <facet>, complete either the route arg or the
// facet arg, whichever is appropriate
func CompleteRouteOrFacet(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) < 1 {
return CompleteRoute(cmd, args, toComplete)
}
return CompleteFacet(cmd, args, toComplete)
}
func CompleteFacet(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
facets, err := GetFacets(cmd, args[0])
if err != nil {
Logger(cmd).Err(err).Send()
return nil, cobra.ShellCompDirectiveError
} else if facets.FacetOptions == nil || len(*facets.FacetOptions) < 1 {
Logger(cmd).Error().Str("route", args[0]).Msg("no facets found for route")
return nil, cobra.ShellCompDirectiveError
}
compFacets := make([]string, 0, len(*facets.FacetOptions))
for _, facet := range *facets.FacetOptions {
if strings.HasPrefix(facet, toComplete) {
compFacets = append(compFacets, facet)
}
}
return slices.Clip(compFacets), cobra.ShellCompDirectiveNoFileComp
}
func CompleteRoute(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
routes := eia.GetRoutes()
compRoutes := make([]string, 0, len(routes))