...
1 package cli
2
3 import (
4 "context"
5 "flag"
6 "strings"
7
8 "github.com/peterbourgon/ff/v3/ffcli"
9
10 dashmgr "edge-infra.dev/pkg/lib/gcp/monitoring/dashboardmanager"
11 )
12
13 type updateArgsT struct {
14 addLabels string
15 remLabels string
16 validate bool
17 name string
18 rename string
19 }
20
21 var updateArgs updateArgsT
22 var updateFlagSet = newUpdateFlagSet(&updateArgs)
23
24 func newUpdateFlagSet(updateArgs *updateArgsT) *flag.FlagSet {
25 updatef := newFlagSet("update")
26 updatef.StringVar(&updateArgs.name, "name", "", "Dashboard Name or Display Name to update. (required)\nNOTE: If a Display Name is used and multiple dashboards exist with the same name, only the first match will be used.")
27 updatef.StringVar(&updateArgs.addLabels, "add-labels", "", "Label(s) to append to the specified dashboard. (optional)\nNOTE: Use comma separated values if specifying more than one label.")
28 updatef.StringVar(&updateArgs.remLabels, "remove-labels", "", "Label(s) to remove from the specified dashboard. (optional)\nNOTE: Use comma separated values if specifying more than one label.")
29 updatef.StringVar(&updateArgs.rename, "new-name", "", "Rename's the dashboard with the new name provided. (optional)")
30 updatef.BoolVar(&updateArgs.validate, "dryrun", false, "Validates the dashboard action without committing the changes. (optional)")
31 return updatef
32 }
33
34 var updateCmd = &ffcli.Command{
35 Name: "update",
36 ShortUsage: "update [flags]",
37 ShortHelp: "Updates existing project dashboard configuration",
38 LongHelp: strings.TrimSpace(`
39 Updates the dashboard instance configuration in the project.
40 `),
41 FlagSet: withGlobalFlags(updateFlagSet),
42 Exec: runUpdate,
43 }
44
45 func runUpdate(ctx context.Context, args []string) error {
46 if len(args) > 0 {
47 Fatalf("too many non-flag arguments: %q", args)
48 }
49 if !checkUpdateFlags() {
50 Println()
51 return flag.ErrHelp
52 }
53
54
55 client, err := dashmgr.New(ctx, projectID)
56 if err != nil {
57 return err
58 }
59
60
61 if len(updateArgs.addLabels) > 0 {
62 labels := strArray(updateArgs.addLabels)
63 err = client.AddLabelsToDashboard(labels, updateArgs.name, updateArgs.validate)
64 if err != nil {
65 return err
66 }
67 }
68
69
70 if len(updateArgs.remLabels) > 0 {
71 labels := strArray(updateArgs.remLabels)
72 err = client.RemoveLabelsFromDashboard(labels, updateArgs.name, updateArgs.validate)
73 if err != nil {
74 return err
75 }
76 }
77
78
79 if len(updateArgs.rename) > 0 && len(updateArgs.name) > 0 {
80 err = client.Rename(updateArgs.rename, updateArgs.name, updateArgs.validate)
81 if err != nil {
82 return err
83 }
84 }
85 return nil
86 }
87
88
89 func checkUpdateFlags() bool {
90 if len(projectID) == 0 {
91 Println("Error: no value specified for [project] - a valid project-id is required")
92 return false
93 }
94
95 if len(updateArgs.name) == 0 {
96 Println("Error: no value specified for [name] - a dashboard name or display name is required")
97 return false
98 }
99
100 if len(updateArgs.addLabels) == 0 && len(updateArgs.remLabels) == 0 && len(updateArgs.rename) == 0 {
101 Println("Error: no values specified for [add-labels], [remove-labels] or [new-name] - please provide at least one parameter to perform an update action on the dashboard")
102 return false
103 }
104
105 return true
106 }
107
View as plain text