/
variables.go
148 lines (124 loc) · 3.35 KB
/
variables.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
138
139
140
141
142
143
144
145
146
147
148
package root
import (
"fmt"
"github.com/c-bata/go-prompt"
"github.com/eclipse/eclipsefuro/furops/internal/root/expressions"
"github.com/eclipse/eclipsefuro/furops/internal/root/suggester"
"log"
"regexp"
"strconv"
"strings"
)
//func (d prompt.Document) []prompt.Suggest{}
func queryVariables(fps FPS) map[string]interface{} {
prompters := map[string]func(d prompt.Document) []prompt.Suggest{
"string": suggester.Stringinput,
"stringlist": suggester.Stringinput,
"type": suggester.Typecompleter,
"service": suggester.Servicecompleter,
"directory": suggester.Directory,
"number": suggester.Number,
"bool": suggester.Bool,
}
res := map[string]interface{}{}
for _, conf := range fps.Variables {
// if condition was set and results to false, skip the var
if conf.Condition != "" {
if expressions.EvaluateExpression(res, conf.Condition) == false {
continue
}
}
if conf.Expression != "" {
res[conf.VarName] = expressions.EvaluateExpression(res, conf.Expression)
continue
}
done := false
p, av := prompters[conf.InputKind]
if !av {
fmt.Println(conf.VarName, " input kind ", conf.InputKind, " not supported")
continue
}
initialText := ""
if conf.Default != "" {
it := expressions.EvaluateExpression(res, conf.Default)
initialText = it.(string)
}
Clear()
for !done {
opts := applyTheme()
opts = append(opts, prompt.OptionInitialBufferText(initialText))
opts = append(opts, prompt.OptionAddKeyBind(prompt.KeyBind{
Key: prompt.ControlC,
Fn: exit,
}))
if conf.InputKind == "directory" {
opts = append(opts, prompt.OptionCompletionWordSeparator("/"))
}
fmt.Println(conf.Prompt)
input := prompt.Input(conf.VarName+": ", p, opts...)
// check for regexp and re query if it is not fulfilled
if conf.Regexp != "" {
if conf.InputKind == "stringlist" {
// stringlist proves each item seperate
var items = strings.Split(input, ",")
var errfound = false
for _, inputItem := range items {
matched, err := regexp.MatchString(conf.Regexp, strings.TrimSpace(inputItem))
if err != nil {
log.Fatal("regexp error for ", conf.VarName, err)
}
errfound = !matched || errfound
if !matched {
fmt.Println("Input", inputItem, " does match pattern")
initialText = input
}
}
done = !errfound
if errfound {
fmt.Println(conf.RegexpText)
fmt.Println("")
}
} else {
matched, err := regexp.MatchString(conf.Regexp, input)
if err != nil {
log.Fatal("regexp error for ", conf.VarName, err)
}
done = matched
if !matched {
fmt.Println("Input ", input, " does match pattern ", conf.RegexpText)
initialText = input
}
}
} else {
done = true
}
switch conf.InputKind {
case "number":
val, err := strconv.ParseFloat(input, 64)
if err == nil {
res[conf.VarName] = val
} else {
res[conf.VarName] = 0
}
break
case "bool":
if input == "true" {
res[conf.VarName] = true
} else {
res[conf.VarName] = false
}
break
case "stringlist":
var items = strings.Split(input, ",")
for index, ele := range items {
items[index] = strings.TrimSpace(ele)
}
res[conf.VarName] = items
break
default:
res[conf.VarName] = input
}
}
}
return res
}