package cli import ( "context" "flag" "strings" "github.com/peterbourgon/ff/v3/ffcli" dashmgr "edge-infra.dev/pkg/lib/gcp/monitoring/dashboardmanager" ) type updateArgsT struct { addLabels string remLabels string validate bool name string rename string } var updateArgs updateArgsT var updateFlagSet = newUpdateFlagSet(&updateArgs) func newUpdateFlagSet(updateArgs *updateArgsT) *flag.FlagSet { updatef := newFlagSet("update") 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.") 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.") 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.") updatef.StringVar(&updateArgs.rename, "new-name", "", "Rename's the dashboard with the new name provided. (optional)") updatef.BoolVar(&updateArgs.validate, "dryrun", false, "Validates the dashboard action without committing the changes. (optional)") return updatef } var updateCmd = &ffcli.Command{ Name: "update", ShortUsage: "update [flags]", ShortHelp: "Updates existing project dashboard configuration", LongHelp: strings.TrimSpace(` Updates the dashboard instance configuration in the project. `), FlagSet: withGlobalFlags(updateFlagSet), Exec: runUpdate, } func runUpdate(ctx context.Context, args []string) error { if len(args) > 0 { Fatalf("too many non-flag arguments: %q", args) } if !checkUpdateFlags() { Println() return flag.ErrHelp } // create the dashboard manager client client, err := dashmgr.New(ctx, projectID) if err != nil { return err } // add dashboard labels if len(updateArgs.addLabels) > 0 { labels := strArray(updateArgs.addLabels) err = client.AddLabelsToDashboard(labels, updateArgs.name, updateArgs.validate) if err != nil { return err } } // remove dashboard labels if len(updateArgs.remLabels) > 0 { labels := strArray(updateArgs.remLabels) err = client.RemoveLabelsFromDashboard(labels, updateArgs.name, updateArgs.validate) if err != nil { return err } } // rename dashboard if len(updateArgs.rename) > 0 && len(updateArgs.name) > 0 { err = client.Rename(updateArgs.rename, updateArgs.name, updateArgs.validate) if err != nil { return err } } return nil } // validates the required update subcommand flags have been provided func checkUpdateFlags() bool { if len(projectID) == 0 { Println("Error: no value specified for [project] - a valid project-id is required") return false } if len(updateArgs.name) == 0 { Println("Error: no value specified for [name] - a dashboard name or display name is required") return false } if len(updateArgs.addLabels) == 0 && len(updateArgs.remLabels) == 0 && len(updateArgs.rename) == 0 { 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") return false } return true }