-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.go
39 lines (34 loc) · 907 Bytes
/
interactive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package goutils
import (
"github.com/tcnksm/go-input"
"os"
"fmt"
)
// Confirm force input string must be in positiveChoices or negativeChoices,
// if false choice is selected, negative action will be invoked.
// This function can be useful that if you want user must make a choice before doing anything further.
func Confirm(query string, positiveChoices, negativeChoices []string, negativeAction func()) {
ui := &input.UI{
Writer: os.Stdout,
Reader: os.Stdin,
}
choices := append(positiveChoices, negativeChoices...)
result, err := ui.Ask(query, &input.Options{
Loop: true,
Required: true,
ValidateFunc: func(input string) error {
if !ContainsStr(choices, input) {
return fmt.Errorf("you must input %s", choices)
}
return nil
},
})
if err != nil {
RedText(err.Error())
os.Exit(1)
}
if ContainsStr(negativeChoices, result) {
negativeAction()
os.Exit(0)
}
}