-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a confirmation prompt for dashboards upload (#174)
* Adding a confirmation prompt for dashboards upload * Address code review comments * Address code review comments
- Loading branch information
1 parent
06a594c
commit 2017f3c
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |