Add facet completion func
This commit is contained in:
parent
42bf0696d6
commit
dbe71e2063
@ -1,6 +1,7 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"github.com/k0kubun/pp/v3"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"gitea.libretechconsulting.com/50W/eia-api-go/cmd/eia-client/internal/util"
|
||||
@ -10,12 +11,18 @@ var ListFacetsCmd = &cobra.Command{
|
||||
Use: "facets route",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "List facets for given API route",
|
||||
ValidArgsFunction: util.CompleteRoutes,
|
||||
PreRun: util.SetClient,
|
||||
ValidArgsFunction: util.CompleteRouteOrFacet,
|
||||
Run: RunListFacetsCmd,
|
||||
}
|
||||
|
||||
func RunListFacetsCmd(cmd *cobra.Command, args []string) {
|
||||
logger := util.Logger(cmd)
|
||||
|
||||
resp, err := util.GetFacets(cmd, args[0])
|
||||
util.Logger(cmd).Info().Any("resp", resp).Err(err).Send()
|
||||
if err != nil {
|
||||
logger.Fatal().
|
||||
Err(err).Str("route", args[0]).Msg("failed to fetch facets")
|
||||
}
|
||||
|
||||
pp.Print(resp)
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"gitea.libretechconsulting.com/50W/eia-api-go/cmd/eia-client/cmd/get"
|
||||
"gitea.libretechconsulting.com/50W/eia-api-go/cmd/eia-client/cmd/list"
|
||||
"gitea.libretechconsulting.com/50W/eia-api-go/cmd/eia-client/internal/util"
|
||||
)
|
||||
@ -42,7 +43,7 @@ var rootCmd = &cobra.Command{
|
||||
PersistentPreRun: PreRun,
|
||||
}
|
||||
|
||||
func PreRun(cmd *cobra.Command, _ []string) {
|
||||
func PreRun(cmd *cobra.Command, args []string) {
|
||||
// Set up logging
|
||||
console := zerolog.ConsoleWriter{
|
||||
Out: os.Stdout,
|
||||
@ -53,13 +54,16 @@ func PreRun(cmd *cobra.Command, _ []string) {
|
||||
|
||||
util.SetLogger(cmd, &logger)
|
||||
|
||||
// Be quiet if we're completing
|
||||
if strings.Contains(cmd.CommandPath(), "comple") {
|
||||
return
|
||||
cmd.Flags().Set(util.FLAG_API_LOG_LEVEL, "trace")
|
||||
} else {
|
||||
util.Logger(cmd).Debug().
|
||||
Str("logLevel", util.Logger(cmd).GetLevel().String()).
|
||||
Msg("logging configured")
|
||||
}
|
||||
|
||||
util.Logger(cmd).Debug().
|
||||
Str("logLevel", util.Logger(cmd).GetLevel().String()).
|
||||
Msg("logging configured")
|
||||
util.SetClient(cmd, args)
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
@ -93,4 +97,5 @@ func init() {
|
||||
|
||||
// Subcommands
|
||||
rootCmd.AddCommand(list.ListCmd)
|
||||
rootCmd.AddCommand(get.GetCmd)
|
||||
}
|
||||
|
@ -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))
|
||||
|
||||
|
2
go.mod
2
go.mod
@ -4,11 +4,11 @@ go 1.23.3
|
||||
|
||||
require (
|
||||
github.com/deepmap/oapi-codegen v1.16.3
|
||||
github.com/k0kubun/pp/v3 v3.3.0
|
||||
github.com/oapi-codegen/oapi-codegen/v2 v2.4.1
|
||||
github.com/oapi-codegen/runtime v1.1.1
|
||||
github.com/rs/zerolog v1.33.0
|
||||
github.com/spf13/cobra v1.8.1
|
||||
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078
|
||||
)
|
||||
|
||||
require (
|
||||
|
4
go.sum
4
go.sum
@ -55,6 +55,8 @@ github.com/invopop/yaml v0.3.1/go.mod h1:PMOp3nn4/12yEZUFfmOuNHJsZToEEOwoWsT+D81
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
|
||||
github.com/k0kubun/pp/v3 v3.3.0 h1:/Unrck5tDGUSjsUJsmx9GUL64pNKOY5UEdoP1F7FBq8=
|
||||
github.com/k0kubun/pp/v3 v3.3.0/go.mod h1:wJadGBvcY6JKaiUkB89VzUACKDmTX1r4aQTPERpZc6w=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
@ -205,5 +207,3 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
|
||||
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 h1:jGnCPejIetjiy2gqaJ5V0NLwTpF4wbQ6cZIItJCSHno=
|
||||
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
|
||||
|
Loading…
Reference in New Issue
Block a user