forked from groovili/go-wagen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteract.go
49 lines (38 loc) · 761 Bytes
/
interact.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
40
41
42
43
44
45
46
47
48
49
package main
import (
"bufio"
"fmt"
"github.com/fatih/color"
"os"
)
type actionFunc func(answer *string) error
type action struct {
Question string
Validate actionFunc
Action actionFunc
}
func userAction(a *action) error {
printMsg(a.Question)
scn := bufio.NewScanner(os.Stdin)
for scn.Scan() {
inp := scn.Text()
if err := a.Validate(&inp); err != nil {
printErr(err.Error())
continue
}
if err := a.Action(&inp); err != nil {
return err
}
break
}
return nil
}
func printErr(msg string) {
fmt.Fprint(os.Stderr, color.RedString("%s\r\n", msg))
}
func printMsg(msg string) {
fmt.Fprint(os.Stdin, fmt.Sprintf("%s\r\n", msg))
}
func printSuccess(msg string) {
fmt.Fprint(os.Stdin, color.GreenString("%s\r\n", msg))
}