-
Notifications
You must be signed in to change notification settings - Fork 4
/
update_agent.go
72 lines (58 loc) · 1.69 KB
/
update_agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package agent
import (
"fmt"
"github.com/spf13/cobra"
cloud "github.com/calyptia/api/types"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
)
func NewCmdUpdateAgent(config *cfg.Config) *cobra.Command {
var newName string
var fleetKey string
var environment string
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "agent AGENT",
Short: "Update a single agent by ID or name",
Args: cobra.ExactArgs(1),
ValidArgsFunction: completer.CompleteAgents,
RunE: func(cmd *cobra.Command, args []string) error {
agentKey := args[0]
agentID, err := completer.LoadAgentID(agentKey, "")
if err != nil {
return err
}
fs := cmd.Flags()
var in cloud.UpdateAgent
if fs.Changed("new-name") {
in.Name = &newName
}
if fs.Changed("environment") {
envID, err := completer.LoadEnvironmentID(environment)
if err != nil {
return err
}
in.EnvironmentID = &envID
}
if fs.Changed("fleet") {
fleetID, err := completer.LoadFleetID(fleetKey)
if err != nil {
return err
}
in.FleetID = &fleetID
}
err = config.Cloud.UpdateAgent(config.Ctx, agentID, in)
if err != nil {
return fmt.Errorf("could not update agent: %w", err)
}
return nil
},
}
fs := cmd.Flags()
fs.StringVar(&newName, "new-name", "", "New agent name")
fs.StringVar(&environment, "environment", "", "Calyptia environment name")
fs.StringVar(&fleetKey, "fleet", "", "Attach this agent to the given fleet")
_ = cmd.RegisterFlagCompletionFunc("environment", completer.CompleteEnvironments)
_ = cmd.RegisterFlagCompletionFunc("fleet", completer.CompleteFleets)
return cmd
}