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

70 lines
1.9 KiB
Go

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"
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/admin/proxy/flag"
"forge.cadoles.com/cadoles/bouncer/internal/format"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
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))
proxies, err := client.QueryLayer(ctx.Context, proxyName, options...)
if err != nil {
return errors.WithStack(apierr.Wrap(err))
}
hints := layerHeaderHints(baseFlags.OutputMode)
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(proxies)...); err != nil {
return errors.WithStack(err)
}
return nil
},
}
}