64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package proxy
|
|
|
|
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"
|
|
"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 proxies",
|
|
Flags: clientFlag.ComposeFlags(
|
|
&cli.Int64SliceFlag{
|
|
Name: "ids",
|
|
Usage: "use `IDS` 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.QueryProxyOptionFunc, 0)
|
|
|
|
rawIDs := ctx.StringSlice("ids")
|
|
if rawIDs != nil {
|
|
proxyIDs := func(ids []string) []store.ProxyID {
|
|
agentIDs := make([]store.ProxyID, len(ids))
|
|
for i, id := range ids {
|
|
agentIDs[i] = store.ProxyID(id)
|
|
}
|
|
return agentIDs
|
|
}(rawIDs)
|
|
options = append(options, client.WithQueryProxyID(proxyIDs...))
|
|
}
|
|
|
|
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
|
|
|
proxies, err := client.QueryProxy(ctx.Context, options...)
|
|
if err != nil {
|
|
return errors.WithStack(apierr.Wrap(err))
|
|
}
|
|
|
|
hints := proxyHeaderHints(baseFlags.OutputMode)
|
|
|
|
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(proxies)...); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|