-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.go
137 lines (99 loc) · 2.81 KB
/
prompt.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package prompt
import (
"errors"
"github.com/manifoldco/promptui"
"strconv"
"sync"
"sync/atomic"
)
// Used to control the access to a prompt method across Goroutines
var mutex sync.Mutex
// Used to share input/selected option across Goroutines
var promptsValues atomic.Value
type Prompt struct{}
// ReadInt Read input as int from a prompt
// and cache it for future use in other Goroutines (if needed)
func (p *Prompt) ReadInt(label string) interface{} {
mutex.Lock()
defer mutex.Unlock()
if value, ok := readInputFromAtomic[int64](label); ok {
return value
}
validate := func(input string) error {
_, err := strconv.ParseInt(input, 10, 64)
if err != nil {
return errors.New("invalid number format")
}
return nil
}
prompt := promptui.Prompt{
Label: label,
Validate: validate,
}
result, err := prompt.Run()
if err != nil {
panic(err)
}
number, _ := strconv.ParseInt(result, 10, 64)
return loadInputInAtomic[int64](label, number)
}
// ReadString Read input as string from a prompt input field (text)
// and cache it for future use in other Goroutines (if needed)
func (p *Prompt) ReadString(label string) interface{} {
mutex.Lock()
defer mutex.Unlock()
if value, ok := readInputFromAtomic[string](label); ok {
return value
}
prompt := promptui.Prompt{
Label: label,
}
result, err := prompt.Run()
if err != nil {
panic(err)
}
return loadInputInAtomic[string](label, result)
}
// Select Read input as string from a prompt input field (text)
// and cache it for future use in other Goroutines (if needed).
func (p *Prompt) Select(label string, options ...string) interface{} {
mutex.Lock()
defer mutex.Unlock()
if value, ok := readInputFromAtomic[string](label); ok {
return value
}
prompt := promptui.Select{
Label: label,
Items: options,
}
_, result, err := prompt.Run()
if err != nil {
panic(err)
}
return loadInputInAtomic[string](label, result)
}
// ReadInputFromAtomic read an input from an atomic map written by other Goroutines
func readInputFromAtomic[T interface{}](label string) (T, bool) {
values := promptsValues.Load()
if values == nil {
inputsCached := make(map[string]interface{}, 100)
promptsValues.Store(inputsCached)
}
inputsCached := promptsValues.Load().(map[string]interface{})
if value, found := inputsCached[label]; found {
return value.(T), true
}
return *new(T), false
}
// LoadInputInAtomic write an input entered into an atomic map to be read by other Goroutines
func loadInputInAtomic[T string | int64 | float32](label string, value T) T {
values := promptsValues.Load()
if values == nil {
inputsCached := make(map[string]interface{}, 100)
promptsValues.Store(inputsCached)
}
inputsCached := promptsValues.Load().(map[string]interface{})
inputsCached[label] = value
promptsValues.Store(inputsCached)
return value
}