Skip to content

Commit

Permalink
Adding a confirmation prompt for dashboards upload (#174)
Browse files Browse the repository at this point in the history
* Adding a confirmation prompt for dashboards upload

* Address code review comments

* Address code review comments
  • Loading branch information
safaci2000 authored Jul 12, 2023
1 parent 06a594c commit 2017f3c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import (
"fmt"
"github.com/esnet/gdg/internal/apphelpers"
"github.com/esnet/gdg/internal/service"
"github.com/esnet/gdg/internal/tools"
"github.com/jedib0t/go-pretty/v6/table"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strings"
)

var (
skipConfirmAction bool
)

func parseDashboardGlobalFlags(cmd *cobra.Command) []string {
folderFilter, _ := cmd.Flags().GetString("folder")
dashboardFilter, _ := cmd.Flags().GetString("dashboard")
Expand Down Expand Up @@ -56,6 +61,13 @@ var uploadDashboard = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {

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

if !skipConfirmAction {
tools.GetUserConfirmation(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(), ", "),
), "", true)
}
grafanaSvc.ExportDashboards(filter)

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

func init() {
rootCmd.AddCommand(dashboard)
dashboard.PersistentFlags().BoolVarP(&skipConfirmAction, "skip-confirmation", "", false, "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
42 changes: 42 additions & 0 deletions internal/tools/prompt_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tools

import (
"bufio"
"fmt"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
"os"
"strings"
)

var validResponse = []rune{'y', 'n'}

// GetUserConfirmation prompts user to confirm operation
// msg Message to prompt the user with
// validate returns true/false on success or terminates the process
// msg: prompt to display to the user asking for a response.
// error: error message to display if app should terminate
// terminate: when set to true will terminate the app user response is not valid.
func GetUserConfirmation(msg, error string, terminate bool) bool {
if error == "" {
error = "Goodbye"
}
for {
fmt.Printf(msg)
r := bufio.NewReader(os.Stdin)
ans, _ := r.ReadString('\n')
ans = strings.ToLower(ans)
if !slices.Contains(validResponse, rune(ans[0])) {
log.Error("Invalid response, please try again. Only [yes/no] are supported")
continue
}
//Validate Response
if ans[0] != 'y' && terminate {
log.Fatal(error)
} else if ans[0] != 'y' {
return false
}
return true
}

}

0 comments on commit 2017f3c

Please sign in to comment.