bouncer/internal/command/client/proxy/layer/query.go

70 lines
1.8 KiB
Go

package layer
import (
"os"
"forge.cadoles.com/cadoles/bouncer/internal/client"
"forge.cadoles.com/cadoles/bouncer/internal/command/client/apierr"
clientFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/flag"
proxyFlag "forge.cadoles.com/cadoles/bouncer/internal/command/client/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: "name",
Usage: "use `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("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
},
}
}