2023-02-28 15:50:35 +01:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2023-03-10 11:28:37 +01:00
|
|
|
agentFlag "forge.cadoles.com/Cadoles/emissary/internal/command/api/agent/flag"
|
|
|
|
"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"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func UpdateCommand() *cli.Command {
|
|
|
|
return &cli.Command{
|
|
|
|
Name: "update",
|
|
|
|
Usage: "Updata agent",
|
|
|
|
Flags: agentFlag.WithAgentFlags(
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "status",
|
|
|
|
Usage: "Set `STATUS` to selected agent",
|
|
|
|
Value: -1,
|
|
|
|
},
|
2023-04-01 13:28:18 +02:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "label",
|
|
|
|
Usage: "Set `LABEL` to selected agent",
|
|
|
|
Value: "",
|
|
|
|
},
|
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-02-28 15:50:35 +01:00
|
|
|
agentID, err := agentFlag.AssertAgentID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
options := make([]client.UpdateAgentOptionFunc, 0)
|
|
|
|
|
|
|
|
status := ctx.Int("status")
|
|
|
|
if status != -1 {
|
|
|
|
options = append(options, client.WithAgentStatus(status))
|
|
|
|
}
|
|
|
|
|
2023-04-01 13:28:18 +02:00
|
|
|
label := ctx.String("label")
|
|
|
|
if label != "" {
|
|
|
|
options = append(options, client.WithAgentLabel(label))
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
client := client.New(baseFlags.ServerURL, client.WithToken(token))
|
2023-02-28 15:50:35 +01:00
|
|
|
|
|
|
|
agent, err := client.UpdateAgent(ctx.Context, agentID, options...)
|
|
|
|
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, agent); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|