Improve route inspection
This commit is contained in:
parent
f661ddcb8b
commit
02d54324f6
1
TODO.md
1
TODO.md
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
## EIA Client Tool
|
## EIA Client Tool
|
||||||
|
|
||||||
|
- [x] Add args and completion for specifying routes for non-final routes
|
||||||
- [x] Add reflection and autocomplete funcs
|
- [x] Add reflection and autocomplete funcs
|
||||||
- [x] Add reflection for listing API Routes under GetV2<stuff here>
|
- [x] Add reflection for listing API Routes under GetV2<stuff here>
|
||||||
- [x] Add reflection for listing route parameters once selected
|
- [x] Add reflection for listing route parameters once selected
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package get
|
package get
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/k0kubun/pp/v3"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/goccy/go-yaml"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"gitea.libretechconsulting.com/50W/eia-api-go/cmd/eia-client/internal/util"
|
"gitea.libretechconsulting.com/50W/eia-api-go/cmd/eia-client/internal/util"
|
||||||
@ -23,5 +25,10 @@ func RunGetFacetCmd(cmd *cobra.Command, args []string) {
|
|||||||
logger.Fatal().Err(err).Send()
|
logger.Fatal().Err(err).Send()
|
||||||
}
|
}
|
||||||
|
|
||||||
pp.Println(facet)
|
bytes, _ := yaml.Marshal(facet)
|
||||||
|
fmt.Println(string(util.PrettyBytes(&util.PrettyOpts{
|
||||||
|
Bytes: append(
|
||||||
|
[]byte(fmt.Sprintf("route: %s\nfacet: %s\n", args[0], args[1])),
|
||||||
|
bytes...),
|
||||||
|
})))
|
||||||
}
|
}
|
||||||
|
@ -34,26 +34,49 @@ var GetRouteCmd = &cobra.Command{
|
|||||||
|
|
||||||
func RunGetRouteCmd(cmd *cobra.Command, args []string) {
|
func RunGetRouteCmd(cmd *cobra.Command, args []string) {
|
||||||
logger := util.Logger(cmd)
|
logger := util.Logger(cmd)
|
||||||
logger.Info().Str("route", args[0]).Msg("getting route description")
|
route := args[0]
|
||||||
|
logger.Info().Str("route", route).Msg("getting route description")
|
||||||
|
|
||||||
finalRoute, route, err := util.GetRoute(cmd, args[0])
|
routeType, err := util.GetRouteType(cmd, route)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal().Err(err).Send()
|
logger.Fatal().Err(err).Send()
|
||||||
}
|
}
|
||||||
|
|
||||||
if route != nil {
|
switch routeType {
|
||||||
bytes, _ := yaml.Marshal(route)
|
case eia.RouteTypeFinal:
|
||||||
fmt.Println(string(util.PrettyBytes(&util.PrettyOpts{
|
showFinalRoute(cmd, route)
|
||||||
Bytes: append([]byte(fmt.Sprintf("name: %s\ntype: routes\n", args[0])), bytes...),
|
case eia.RouteTypeRoutes:
|
||||||
})))
|
showRoutes(cmd, route)
|
||||||
|
default:
|
||||||
|
logger.Fatal().Any("type", routeType).
|
||||||
|
Msg("not a valid route type for get route command")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func showFinalRoute(cmd *cobra.Command, route string) {
|
||||||
|
finalRoute, _, err := util.GetRoute(cmd, route)
|
||||||
|
if err != nil {
|
||||||
|
util.Logger(cmd).Fatal().Err(err).Send()
|
||||||
}
|
}
|
||||||
|
|
||||||
if finalRoute != nil {
|
bytes, _ := yaml.Marshal(finalRoute)
|
||||||
bytes, _ := yaml.Marshal(finalRoute)
|
showRoute(bytes, route, eia.RouteTypeFinal)
|
||||||
fmt.Println(string(util.PrettyBytes(&util.PrettyOpts{
|
}
|
||||||
Bytes: append([]byte(fmt.Sprintf("name: %s\ntype: finalRoute\n", args[0])), bytes...),
|
|
||||||
})))
|
func showRoutes(cmd *cobra.Command, route string) {
|
||||||
|
_, routes, err := util.GetRoute(cmd, route)
|
||||||
|
if err != nil {
|
||||||
|
util.Logger(cmd).Fatal().Err(err).Send()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bytes, _ := yaml.Marshal(routes)
|
||||||
|
showRoute(bytes, route, eia.RouteTypeRoutes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func showRoute(bytes []byte, name string, routeType eia.RouteType) {
|
||||||
|
fmt.Println(string(util.PrettyBytes(&util.PrettyOpts{
|
||||||
|
Bytes: append([]byte(fmt.Sprintf("name: %s\ntype: %s\n", name, routeType.String())), bytes...),
|
||||||
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
func completeRouteRoutes(cmd *cobra.Command, args []string, toComplete string) (
|
func completeRouteRoutes(cmd *cobra.Command, args []string, toComplete string) (
|
||||||
|
@ -18,6 +18,7 @@ var listRoutesCmd = &cobra.Command{
|
|||||||
ValidArgs: []string{
|
ValidArgs: []string{
|
||||||
"Data",
|
"Data",
|
||||||
"Facet",
|
"Facet",
|
||||||
|
"FacetId",
|
||||||
},
|
},
|
||||||
Run: RunListRoutesCmd,
|
Run: RunListRoutesCmd,
|
||||||
}
|
}
|
||||||
@ -28,38 +29,52 @@ const (
|
|||||||
routePrefixFlag = "routePrefix"
|
routePrefixFlag = "routePrefix"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var defaultExcludedSuffixes = []string{
|
||||||
|
"WithBody",
|
||||||
|
"Facet",
|
||||||
|
"FacetId",
|
||||||
|
}
|
||||||
|
|
||||||
func RunListRoutesCmd(cmd *cobra.Command, args []string) {
|
func RunListRoutesCmd(cmd *cobra.Command, args []string) {
|
||||||
logger := util.Logger(cmd)
|
logger := util.Logger(cmd)
|
||||||
|
|
||||||
// Command flags
|
// Command flags
|
||||||
allRoutes, _ := cmd.Flags().GetBool(allMethodsFlag)
|
allRoutes, _ := cmd.Flags().GetBool(allMethodsFlag)
|
||||||
routePrefix, _ := cmd.Flags().GetString(routePrefixFlag)
|
routePrefix, _ := cmd.Flags().GetString(routePrefixFlag)
|
||||||
filters, _ := cmd.Flags().GetStringSlice(filtersFlag)
|
userFilters, _ := cmd.Flags().GetStringSlice(filtersFlag)
|
||||||
|
|
||||||
if allRoutes && len(args) > 0 {
|
if allRoutes && len(args) > 0 {
|
||||||
logger.Fatal().Msg("can't specify all methods while also filtering by type")
|
logger.Fatal().Msg("can't specify all methods while also filtering by type")
|
||||||
}
|
}
|
||||||
|
|
||||||
filter := make([]string, 0)
|
typeFilter := make([]string, 0)
|
||||||
if !allRoutes && len(args) == 1 {
|
if !allRoutes && len(args) == 1 {
|
||||||
filter = append(filter, args[0])
|
typeFilter = append(typeFilter, args[0])
|
||||||
} else if !allRoutes {
|
|
||||||
filter = append(filter, "Data")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
routes := eia.GetRoutes(filter...)
|
routes := eia.GetRoutes(typeFilter...)
|
||||||
filteredRoutes := make([]string, 0, len(routes))
|
filteredRoutes := make([]string, 0, len(routes))
|
||||||
|
|
||||||
// Apply filters for routePrefix, and optional filters
|
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
|
// Filter prefixes
|
||||||
if !allRoutes && routePrefix != "" {
|
if !allRoutes && routePrefix != "" {
|
||||||
if !strings.HasPrefix(strings.ToLower(route), strings.ToLower(routePrefix)) {
|
if !strings.HasPrefix(strings.ToLower(route), strings.ToLower(routePrefix)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(filters) > 0 {
|
// Default exclusions
|
||||||
for _, f := range filters {
|
if !allRoutes && len(typeFilter) < 1 && len(userFilters) < 1 {
|
||||||
|
for _, e := range defaultExcludedSuffixes {
|
||||||
|
if strings.HasSuffix(strings.ToLower(route), strings.ToLower(e)) {
|
||||||
|
goto next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// User inclusion filters
|
||||||
|
if len(userFilters) > 0 {
|
||||||
|
for _, f := range userFilters {
|
||||||
if strings.Contains(strings.ToLower(route), strings.ToLower(f)) {
|
if strings.Contains(strings.ToLower(route), strings.ToLower(f)) {
|
||||||
filteredRoutes = append(filteredRoutes, route)
|
filteredRoutes = append(filteredRoutes, route)
|
||||||
}
|
}
|
||||||
@ -67,6 +82,7 @@ func RunListRoutesCmd(cmd *cobra.Command, args []string) {
|
|||||||
} else {
|
} else {
|
||||||
filteredRoutes = append(filteredRoutes, route)
|
filteredRoutes = append(filteredRoutes, route)
|
||||||
}
|
}
|
||||||
|
next:
|
||||||
}
|
}
|
||||||
|
|
||||||
pp.Println(filteredRoutes)
|
pp.Println(filteredRoutes)
|
||||||
@ -74,6 +90,6 @@ func RunListRoutesCmd(cmd *cobra.Command, args []string) {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
listRoutesCmd.PersistentFlags().BoolP(allMethodsFlag, "a", false, "List all methods, no filtering")
|
listRoutesCmd.PersistentFlags().BoolP(allMethodsFlag, "a", false, "List all methods, no filtering")
|
||||||
listRoutesCmd.PersistentFlags().StringSliceP(filtersFlag, "f", []string{}, "Optional filters, case insensitive")
|
listRoutesCmd.PersistentFlags().StringSliceP(filtersFlag, "f", []string{}, "Optional [inclusion] filters, case insensitive")
|
||||||
listRoutesCmd.PersistentFlags().StringP(routePrefixFlag, "p", "Get", "Prefix for routes, ignore with -a flag")
|
listRoutesCmd.PersistentFlags().StringP(routePrefixFlag, "p", "Get", "Prefix for routes, ignore with -a flag")
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,14 @@ import (
|
|||||||
"gitea.libretechconsulting.com/50W/eia-api-go/pkg/eia"
|
"gitea.libretechconsulting.com/50W/eia-api-go/pkg/eia"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetRouteType(cmd *cobra.Command, route string) (eia.RouteType, error) {
|
||||||
|
client, err := Client(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return eia.RouteTypeError, err
|
||||||
|
}
|
||||||
|
return client.GetRouteType(cmd.Context(), route, eia.DefaultMethodSubs(cmd.Context()))
|
||||||
|
}
|
||||||
|
|
||||||
func GetRoute(cmd *cobra.Command, route string) (*eiaapi.FinalRoute, *eiaapi.Routes, error) {
|
func GetRoute(cmd *cobra.Command, route string) (*eiaapi.FinalRoute, *eiaapi.Routes, error) {
|
||||||
client, err := Client(cmd)
|
client, err := Client(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
2
go.mod
2
go.mod
@ -11,6 +11,7 @@ require (
|
|||||||
github.com/oapi-codegen/runtime v1.1.1
|
github.com/oapi-codegen/runtime v1.1.1
|
||||||
github.com/rs/zerolog v1.33.0
|
github.com/rs/zerolog v1.33.0
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.8.1
|
||||||
|
golang.org/x/tools v0.27.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@ -35,7 +36,6 @@ require (
|
|||||||
golang.org/x/sync v0.9.0 // indirect
|
golang.org/x/sync v0.9.0 // indirect
|
||||||
golang.org/x/sys v0.27.0 // indirect
|
golang.org/x/sys v0.27.0 // indirect
|
||||||
golang.org/x/text v0.20.0 // indirect
|
golang.org/x/text v0.20.0 // indirect
|
||||||
golang.org/x/tools v0.27.0 // indirect
|
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
2
go.sum
2
go.sum
@ -59,8 +59,6 @@ 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 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
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/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/k0kubun/pp/v3 v3.4.1 h1:1WdFZDRRqe8UsR61N/2RoOZ3ziTEqgTPVqKrHeb779Y=
|
github.com/k0kubun/pp/v3 v3.4.1 h1:1WdFZDRRqe8UsR61N/2RoOZ3ziTEqgTPVqKrHeb779Y=
|
||||||
github.com/k0kubun/pp/v3 v3.4.1/go.mod h1:+SiNiqKnBfw1Nkj82Lh5bIeKQOAkPy6Xw9CAZUZ8npI=
|
github.com/k0kubun/pp/v3 v3.4.1/go.mod h1:+SiNiqKnBfw1Nkj82Lh5bIeKQOAkPy6Xw9CAZUZ8npI=
|
||||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
|
@ -41,6 +41,34 @@ var defaultMethodSubs = MethodSubs{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:generate stringer -type=RouteType
|
||||||
|
type RouteType uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
RouteTypeRoutes RouteType = iota
|
||||||
|
RouteTypeFinal
|
||||||
|
RouteTypeFacets
|
||||||
|
RouteTypeNotFound
|
||||||
|
RouteTypeError
|
||||||
|
)
|
||||||
|
|
||||||
|
// Checks the route to see if it returns a facet, a list of routes,
|
||||||
|
// or a final route
|
||||||
|
func (client *Client) GetRouteType(ctx context.Context, route string, subs *MethodSubs) (RouteType, error) {
|
||||||
|
if facets, err := client.GetFacets(ctx, route, subs); err == nil && facets != nil {
|
||||||
|
return RouteTypeFacets, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
finalRoute, routes, err := client.GetRoutesOrFinalRoute(ctx, route, subs)
|
||||||
|
if finalRoute != nil {
|
||||||
|
return RouteTypeFinal, nil
|
||||||
|
} else if routes != nil {
|
||||||
|
return RouteTypeRoutes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return RouteTypeNotFound, err
|
||||||
|
}
|
||||||
|
|
||||||
// Retrieve information for a named Route (e.g. GetAeoV2Route1)
|
// Retrieve information for a named Route (e.g. GetAeoV2Route1)
|
||||||
// Returns a *eiaapi.Routes if this is not a final route, otherwise returns
|
// Returns a *eiaapi.Routes if this is not a final route, otherwise returns
|
||||||
// a final route response
|
// a final route response
|
||||||
|
27
pkg/eia/routetype_string.go
Normal file
27
pkg/eia/routetype_string.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// Code generated by "stringer -type=RouteType"; DO NOT EDIT.
|
||||||
|
|
||||||
|
package eia
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
|
// Re-run the stringer command to generate them again.
|
||||||
|
var x [1]struct{}
|
||||||
|
_ = x[RouteTypeRoutes-0]
|
||||||
|
_ = x[RouteTypeFinal-1]
|
||||||
|
_ = x[RouteTypeFacets-2]
|
||||||
|
_ = x[RouteTypeNotFound-3]
|
||||||
|
_ = x[RouteTypeError-4]
|
||||||
|
}
|
||||||
|
|
||||||
|
const _RouteType_name = "RouteTypeRoutesRouteTypeFinalRouteTypeFacetsRouteTypeNotFoundRouteTypeError"
|
||||||
|
|
||||||
|
var _RouteType_index = [...]uint8{0, 15, 29, 44, 61, 75}
|
||||||
|
|
||||||
|
func (i RouteType) String() string {
|
||||||
|
if i >= RouteType(len(_RouteType_index)-1) {
|
||||||
|
return "RouteType(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||||
|
}
|
||||||
|
return _RouteType_name[_RouteType_index[i]:_RouteType_index[i+1]]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user