package proxy import ( "os" "slices" "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 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) rawNames := ctx.StringSlice("ids") if rawNames != nil { proxyNames := func(names []string) []store.ProxyName { proxyNames := make([]store.ProxyName, len(names)) for i, name := range names { proxyNames[i] = store.ProxyName(name) } return proxyNames }(rawNames) options = append(options, client.WithQueryProxyNames(proxyNames...)) } client := client.New(baseFlags.ServerURL, client.WithToken(token)) proxies, err := client.QueryProxy(ctx.Context, options...) if err != nil { return errors.WithStack(apierr.Wrap(err)) } slices.SortFunc(proxies, sortProxiesByWeight) hints := proxyHeaderHints(baseFlags.OutputMode) if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(proxies)...); err != nil { return errors.WithStack(err) } return nil }, } } func sortProxiesByWeight(a *store.ProxyHeader, b *store.ProxyHeader) int { if a.Weight < b.Weight { return 1 } if a.Weight > b.Weight { return -1 } return 0 }