-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmonitor_clone.go
More file actions
64 lines (53 loc) · 1.91 KB
/
monitor_clone.go
File metadata and controls
64 lines (53 loc) · 1.91 KB
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
package monitor
import (
"fmt"
"github.com/spf13/cobra"
sts "github.com/stackvista/stackstate-cli/generated/stackstate_api"
stscobra "github.com/stackvista/stackstate-cli/internal/cobra"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
)
type CloneArgs struct {
IdArgs
NewName string
}
func MonitorCloneCommand(cli *di.Deps) *cobra.Command {
args := &CloneArgs{}
cmd := &cobra.Command{
Use: "clone",
Short: "Clone an existing monitor to create an editable copy",
Long: `Clone an existing monitor to create a new copy with a different name. This is useful for creating custom versions of locked monitors from StackPacks.`,
Example: `# clone a monitor by ID
sts monitor clone --id 123456789 --name "My Custom Monitor"
# clone a monitor by identifier
sts monitor clone --identifier urn:stackpack:my-monitor --name "Custom Copy"`,
RunE: cli.CmdRunEWithApi(RunMonitorCloneCommand(args)),
}
common.AddIDFlagVar(cmd, &args.ID, IDFlagUsage)
common.AddIdentifierFlagVar(cmd, &args.Identifier, IdentifierFlagUsage)
common.AddRequiredNameFlagVar(cmd, &args.NewName, NameFlagUsage)
stscobra.MarkMutexFlags(cmd, []string{common.IDFlag, common.IdentifierFlag}, "identifier", true)
return cmd
}
func RunMonitorCloneCommand(args *CloneArgs) di.CmdWithApiFn {
return func(
cmd *cobra.Command,
cli *di.Deps,
api *sts.APIClient,
serverInfo *sts.ServerInfo,
) common.CLIError {
identifier := IdOrIdentifier(args.ID, args.Identifier)
clonedMonitor, resp, err := api.NodeApi.Clone(cli.Context, "Monitor", identifier).NodeName(sts.NodeName{Name: args.NewName}).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"monitor": clonedMonitor,
})
} else {
cli.Printer.Success(fmt.Sprintf("Monitor %d has been created", clonedMonitor.GetId()))
}
return nil
}
}