Skip to content

Commit

Permalink
Adding a confirmation prompt for dashboards upload
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Jul 7, 2023
1 parent 06a594c commit 590cf40
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cmd/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cmd

import (
"bufio"
"fmt"
"github.com/esnet/gdg/internal/apphelpers"
"github.com/esnet/gdg/internal/service"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
"strings"
)

Expand Down Expand Up @@ -48,6 +50,18 @@ var clearDashboards = &cobra.Command{
},
}

// askForConfirmation prompts user to confirm operation
func askForConfirmation(msg string) {
fmt.Printf(msg)
r := bufio.NewReader(os.Stdin)
ans, _ := r.ReadString('\n')
response := strings.ToLower(ans)
if response[0] != 'y' && response[0] != 'Y' {
log.Infof("Goodbye")
os.Exit(0)
}
}

var uploadDashboard = &cobra.Command{
Use: "upload",
Short: "upload all dashboards to grafana",
Expand All @@ -56,6 +70,13 @@ var uploadDashboard = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

filter := service.NewDashboardFilter(parseDashboardGlobalFlags(cmd)...)

confirm, _ := cmd.Flags().GetBool("skip-confirmation")
if confirm {
askForConfirmation(fmt.Sprintf("WARNING: this will delete all dashboards from the monitored folders: '%s' "+
"(or all folders if ignore_dashboard_filters is set to true) and upload your local copy. Do you wish to "+
"continue (y/n)", strings.Join(apphelpers.GetCtxDefaultGrafanaConfig().GetMonitoredFolders(), ", ")))
}
grafanaSvc.ExportDashboards(filter)

tableObj.AppendHeader(table.Row{"Title", "id", "folder", "UID"})
Expand Down Expand Up @@ -120,6 +141,7 @@ var listDashboards = &cobra.Command{

func init() {
rootCmd.AddCommand(dashboard)
dashboard.PersistentFlags().BoolP("skip-confirmation", "", true, "when set to true, bypass confirmation prompts")
dashboard.PersistentFlags().StringP("dashboard", "d", "", "filter by dashboard slug")
dashboard.PersistentFlags().StringP("folder", "f", "", "Filter by Folder Name (Quotes in names not supported)")
dashboard.PersistentFlags().StringSliceP("tags", "t", []string{}, "Filter by Tags (does not apply on upload)")
Expand Down

0 comments on commit 590cf40

Please sign in to comment.