forked from oxequa/interact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteract.go
89 lines (79 loc) · 1.36 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package interact
import (
"io"
)
type (
Interview interface {
//Next()
//Prev()
//Goto(interface{})
}
interview struct {
index int
interact *Interact
current current
}
current struct {
list []*Question
index int
}
)
// Interact element
type Interact struct {
settings
Questions []*Question
After, Before ErrorFunc
}
// General settings and options
type settings struct {
skip bool
err interface{}
prefix
def
end
}
// Answer end
type end struct {
status bool
value string
}
// Default answer value
type def struct {
Value interface{}
Text interface{}
}
// Questions prefix
type prefix struct {
Writer io.Writer
Text interface{}
}
// Run a questions list
func Run(i *Interact) error {
if err := i.ask(); err != nil {
return err
}
return nil
}
// Create a new interact configuration
func New(i *Interact) Interview {
return &interview{interact: i, current: current{index: 0, list: i.Questions}}
}
// Ask one by one with interact after/before
func (i *Interact) ask() (err error) {
context := &context{i: i}
if err := context.method(i.Before); err != nil {
return err
}
if !context.i.skip {
for index := range i.Questions {
i.Questions[index].interact = i
if err = i.Questions[index].ask(); err != nil {
return err
}
}
if err := context.method(i.After); err != nil {
return err
}
}
return nil
}