2023-02-28 15:50:35 +01:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2024-02-27 14:14:30 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/command/client/apierr"
|
|
|
|
clientFlag "forge.cadoles.com/Cadoles/emissary/internal/command/client/flag"
|
2023-03-27 11:05:57 +02:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
2023-06-25 19:45:43 +02:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/pkg/client"
|
2023-02-28 15:50:35 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli/v2"
|
2024-02-26 18:20:40 +01:00
|
|
|
"gitlab.com/wpetit/goweb/cli/format"
|
2023-02-28 15:50:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func QueryCommand() *cli.Command {
|
|
|
|
return &cli.Command{
|
|
|
|
Name: "query",
|
|
|
|
Usage: "Query agents",
|
2023-03-27 11:05:57 +02:00
|
|
|
Flags: clientFlag.ComposeFlags(
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "thumbprints",
|
|
|
|
Usage: "use `THUMBPRINTS` as query filter",
|
|
|
|
Value: nil,
|
|
|
|
},
|
|
|
|
&cli.Int64SliceFlag{
|
|
|
|
Name: "statuses",
|
|
|
|
Usage: "use `STATUSES` as query filter",
|
|
|
|
},
|
|
|
|
&cli.Int64SliceFlag{
|
|
|
|
Name: "ids",
|
|
|
|
Usage: "use `IDS` as query filter",
|
|
|
|
},
|
|
|
|
),
|
2023-02-28 15:50:35 +01:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2023-03-27 11:05:57 +02:00
|
|
|
options := make([]client.QueryAgentsOptionFunc, 0)
|
|
|
|
|
|
|
|
thumbprints := ctx.StringSlice("thumbprints")
|
|
|
|
if thumbprints != nil {
|
|
|
|
options = append(options, client.WithQueryAgentsThumbprints(thumbprints...))
|
|
|
|
}
|
|
|
|
|
|
|
|
rawIDs := ctx.Int64Slice("ids")
|
|
|
|
if rawIDs != nil {
|
|
|
|
agentIDs := func(ids []int64) []datastore.AgentID {
|
|
|
|
agentIDs := make([]datastore.AgentID, len(ids))
|
|
|
|
for i, id := range ids {
|
|
|
|
agentIDs[i] = datastore.AgentID(id)
|
|
|
|
}
|
|
|
|
return agentIDs
|
|
|
|
}(rawIDs)
|
|
|
|
options = append(options, client.WithQueryAgentsID(agentIDs...))
|
|
|
|
}
|
|
|
|
|
|
|
|
rawStatuses := ctx.Int64Slice("statuses")
|
|
|
|
if rawStatuses != nil {
|
|
|
|
statuses := func(rawStatuses []int64) []datastore.AgentStatus {
|
|
|
|
statuses := make([]datastore.AgentStatus, len(rawStatuses))
|
|
|
|
for i, status := range rawStatuses {
|
|
|
|
statuses[i] = datastore.AgentStatus(status)
|
|
|
|
}
|
|
|
|
return statuses
|
|
|
|
}(rawStatuses)
|
|
|
|
options = append(options, client.WithQueryAgentsStatus(statuses...))
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
2023-02-28 15:50:35 +01:00
|
|
|
|
2023-03-27 11:05:57 +02:00
|
|
|
agents, _, err := client.QueryAgents(ctx.Context, options...)
|
2023-02-28 15:50:35 +01:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|