bouncer/internal/command/admin/definition/layer/query.go

64 lines
1.7 KiB
Go
Raw Normal View History

2024-05-17 15:44:28 +02:00
package layer
import (
"os"
"forge.cadoles.com/cadoles/bouncer/internal/client"
"forge.cadoles.com/cadoles/bouncer/internal/command/admin/apierr"
clientFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/flag"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"gitlab.com/wpetit/goweb/cli/format"
)
func QueryCommand() *cli.Command {
return &cli.Command{
Name: "query",
Usage: "Query layer definitions",
Flags: clientFlag.ComposeFlags(
&cli.StringSliceFlag{
Name: "with-type",
Usage: "use `WITH_TYPE` as query filter",
},
),
Action: func(ctx *cli.Context) error {
baseFlags := clientFlag.GetBaseFlags(ctx)
token, err := clientFlag.GetToken(baseFlags)
if err != nil {
return errors.WithStack(apierr.Wrap(err))
}
options := make([]client.QueryLayerDefinitionOptionFunc, 0)
rawTypes := ctx.StringSlice("with-type")
if len(rawTypes) > 0 {
layerTypes := func(rawLayerTypes []string) []store.LayerType {
layerTypes := make([]store.LayerType, len(rawLayerTypes))
for i, layerType := range rawLayerTypes {
layerTypes[i] = store.LayerType(layerType)
}
return layerTypes
}(rawTypes)
options = append(options, client.WithQueryLayerDefinitionTypes(layerTypes...))
}
client := client.New(baseFlags.ServerURL, client.WithToken(token))
proxies, err := client.QueryLayerDefinition(ctx.Context, options...)
if err != nil {
return errors.WithStack(apierr.Wrap(err))
}
hints := layerDefinitionHints(baseFlags.OutputMode)
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(proxies)...); err != nil {
return errors.WithStack(err)
}
return nil
},
}
}