Improve route inspection
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
package get
|
||||
|
||||
import (
|
||||
"github.com/k0kubun/pp/v3"
|
||||
"fmt"
|
||||
|
||||
"github.com/goccy/go-yaml"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"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()
|
||||
}
|
||||
|
||||
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) {
|
||||
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 {
|
||||
logger.Fatal().Err(err).Send()
|
||||
}
|
||||
|
||||
if route != nil {
|
||||
bytes, _ := yaml.Marshal(route)
|
||||
fmt.Println(string(util.PrettyBytes(&util.PrettyOpts{
|
||||
Bytes: append([]byte(fmt.Sprintf("name: %s\ntype: routes\n", args[0])), bytes...),
|
||||
})))
|
||||
switch routeType {
|
||||
case eia.RouteTypeFinal:
|
||||
showFinalRoute(cmd, route)
|
||||
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)
|
||||
fmt.Println(string(util.PrettyBytes(&util.PrettyOpts{
|
||||
Bytes: append([]byte(fmt.Sprintf("name: %s\ntype: finalRoute\n", args[0])), bytes...),
|
||||
})))
|
||||
bytes, _ := yaml.Marshal(finalRoute)
|
||||
showRoute(bytes, route, eia.RouteTypeFinal)
|
||||
}
|
||||
|
||||
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) (
|
||||
|
@ -18,6 +18,7 @@ var listRoutesCmd = &cobra.Command{
|
||||
ValidArgs: []string{
|
||||
"Data",
|
||||
"Facet",
|
||||
"FacetId",
|
||||
},
|
||||
Run: RunListRoutesCmd,
|
||||
}
|
||||
@ -28,38 +29,52 @@ const (
|
||||
routePrefixFlag = "routePrefix"
|
||||
)
|
||||
|
||||
var defaultExcludedSuffixes = []string{
|
||||
"WithBody",
|
||||
"Facet",
|
||||
"FacetId",
|
||||
}
|
||||
|
||||
func RunListRoutesCmd(cmd *cobra.Command, args []string) {
|
||||
logger := util.Logger(cmd)
|
||||
|
||||
// Command flags
|
||||
allRoutes, _ := cmd.Flags().GetBool(allMethodsFlag)
|
||||
routePrefix, _ := cmd.Flags().GetString(routePrefixFlag)
|
||||
filters, _ := cmd.Flags().GetStringSlice(filtersFlag)
|
||||
userFilters, _ := cmd.Flags().GetStringSlice(filtersFlag)
|
||||
|
||||
if allRoutes && len(args) > 0 {
|
||||
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 {
|
||||
filter = append(filter, args[0])
|
||||
} else if !allRoutes {
|
||||
filter = append(filter, "Data")
|
||||
typeFilter = append(typeFilter, args[0])
|
||||
}
|
||||
|
||||
routes := eia.GetRoutes(filter...)
|
||||
routes := eia.GetRoutes(typeFilter...)
|
||||
filteredRoutes := make([]string, 0, len(routes))
|
||||
|
||||
// Apply filters for routePrefix, and optional filters
|
||||
for _, route := range routes {
|
||||
// Filter prefixes
|
||||
if !allRoutes && routePrefix != "" {
|
||||
if !strings.HasPrefix(strings.ToLower(route), strings.ToLower(routePrefix)) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if len(filters) > 0 {
|
||||
for _, f := range filters {
|
||||
// Default exclusions
|
||||
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)) {
|
||||
filteredRoutes = append(filteredRoutes, route)
|
||||
}
|
||||
@ -67,6 +82,7 @@ func RunListRoutesCmd(cmd *cobra.Command, args []string) {
|
||||
} else {
|
||||
filteredRoutes = append(filteredRoutes, route)
|
||||
}
|
||||
next:
|
||||
}
|
||||
|
||||
pp.Println(filteredRoutes)
|
||||
@ -74,6 +90,6 @@ func RunListRoutesCmd(cmd *cobra.Command, args []string) {
|
||||
|
||||
func init() {
|
||||
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")
|
||||
}
|
||||
|
@ -7,6 +7,14 @@ import (
|
||||
"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) {
|
||||
client, err := Client(cmd)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user