2023-02-28 15:50:35 +01:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/client"
|
2023-03-10 11:28:37 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/command/api/apierr"
|
|
|
|
clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/flag"
|
2023-02-28 15:50:35 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/format"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func QueryCommand() *cli.Command {
|
|
|
|
return &cli.Command{
|
|
|
|
Name: "query",
|
|
|
|
Usage: "Query agents",
|
|
|
|
Flags: clientFlag.ComposeFlags(),
|
|
|
|
Action: func(ctx *cli.Context) error {
|
|
|
|
baseFlags := clientFlag.GetBaseFlags(ctx)
|
2023-03-07 23:10:42 +01:00
|
|
|
|
|
|
|
token, err := clientFlag.GetToken(baseFlags)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(apierr.Wrap(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
2023-02-28 15:50:35 +01:00
|
|
|
|
|
|
|
agents, _, err := client.QueryAgents(ctx.Context)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(apierr.Wrap(err))
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
hints := agentHints(baseFlags.OutputMode)
|
2023-02-28 15:50:35 +01:00
|
|
|
|
|
|
|
if err := format.Write(baseFlags.Format, os.Stdout, hints, clientFlag.AsAnySlice(agents)...); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|