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

83 lines
2.1 KiB
Go
Raw Normal View History

2023-04-24 20:52:12 +02:00
package layer
import (
"os"
2024-06-26 16:22:30 +02:00
"slices"
2023-04-24 20:52:12 +02:00
"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"
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/proxy/flag"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
2024-05-17 15:44:28 +02:00
"gitlab.com/wpetit/goweb/cli/format"
2023-04-24 20:52:12 +02:00
)
func QueryCommand() *cli.Command {
return &cli.Command{
Name: "query",
Usage: "Query layers",
Flags: proxyFlag.WithProxyFlags(
&cli.StringSliceFlag{
Name: "with-name",
Usage: "use `WITH_NAME` 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))
}
proxyName, err := proxyFlag.AssertProxyName(ctx)
if err != nil {
return errors.WithStack(err)
}
options := make([]client.QueryLayerOptionFunc, 0)
rawNames := ctx.StringSlice("with-name")
if rawNames != nil {
layerNames := func(names []string) []store.LayerName {
layerNames := make([]store.LayerName, len(names))
for i, name := range names {
layerNames[i] = store.LayerName(name)
}
return layerNames
}(rawNames)
options = append(options, client.WithQueryLayerNames(layerNames...))
}
client := client.New(baseFlags.ServerURL, client.WithToken(token))
2024-06-26 16:22:30 +02:00
layers, err := client.QueryLayer(ctx.Context, proxyName, options...)
2023-04-24 20:52:12 +02:00
if err != nil {
return errors.WithStack(apierr.Wrap(err))
}
2024-06-26 16:22:30 +02:00
slices.SortFunc(layers, sortLayerssByWeight)
2023-04-24 20:52:12 +02:00
hints := layerHeaderHints(baseFlags.OutputMode)
2024-06-26 16:22:30 +02:00
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(layers)...); err != nil {
2023-04-24 20:52:12 +02:00
return errors.WithStack(err)
}
return nil
},
}
}
2024-06-26 16:22:30 +02:00
func sortLayerssByWeight(a *store.LayerHeader, b *store.LayerHeader) int {
if a.Weight < b.Weight {
return 1
}
if a.Weight > b.Weight {
return -1
}
return 0
}