eia-api-go/cmd/eia-client/internal/util/util_completion.go
Ryan McGuire 7431eb94f1
All checks were successful
Build and Publish / build (push) Successful in 46s
Build and Publish / publish (push) Successful in 5s
Move to public repo
2024-12-17 16:02:58 -05:00

53 lines
1.5 KiB
Go

package util
import (
"slices"
"strings"
"github.com/spf13/cobra"
"gitea.libretechconsulting.com/rmcguire/eia-api-go/pkg/eia"
)
// 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("Facet")
compRoutes := make([]string, 0, len(routes))
for _, r := range routes {
if strings.HasPrefix(r, toComplete) {
compRoutes = append(compRoutes, r)
}
}
return slices.Clip(compRoutes), cobra.ShellCompDirectiveNoFileComp
}