-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.go
121 lines (106 loc) · 2.87 KB
/
features.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
package main
import (
"github.com/rivo/tview"
"gopkg.in/yaml.v3"
"strconv"
)
func initFlexFeatures() {
formFeatures := tview.NewForm()
formFeatures.SetTitle("Set Features").SetBorder(true)
newVars := make(map[string]any)
mapYaml := make(map[string]string)
sliceYaml := make(map[string]string)
for _, oneVar := range appConfig.Configurable_vars {
var key string
var value any
for key, value = range oneVar["var"].(map[string]any) {
break
}
// Load value from inventory if exists
if extraVars[key] != nil {
value = extraVars[key]
}
newVars[key] = value
switch value.(type) {
case bool:
formFeatures.AddCheckbox(oneVar["description"].(string), value.(bool), func(checked bool) {
if checked {
newVars[key] = true
} else {
newVars[key] = false
}
})
case []any:
valueByte, err := yaml.Marshal(&value)
check(err)
valueString := string(valueByte)
formFeatures.AddTextArea(oneVar["description"].(string), valueString, 0, 0, 0, func(text string) {
sliceYaml[key] = text
})
case map[string]any:
valueByte, err := yaml.Marshal(&value)
check(err)
valueString := string(valueByte)
formFeatures.AddTextArea(oneVar["description"].(string), valueString, 0, 0, 0, func(text string) {
mapYaml[key] = text
})
case string:
formFeatures.AddInputField(oneVar["description"].(string), value.(string), 0, nil, func(text string) {
newVars[key] = text
})
case int:
valueString := strconv.Itoa(value.(int))
formFeatures.AddInputField(oneVar["description"].(string), valueString, 0, tview.InputFieldInteger, func(text string) {
i, err := strconv.Atoi(text)
if err == nil {
newVars[key] = i
}
})
default:
panic("Can't recognize configurable var: " + key)
}
}
formDown := tview.NewForm()
formDown.AddButton("Save & Next", func() {
for key, value := range sliceYaml {
var valueSlice []any
err := yaml.Unmarshal([]byte(value), &valueSlice)
if err != nil {
showErrorModal(key+" has wrong format.",
func(buttonIndex int, buttonLabel string) {
pages.SwitchToPage("Features")
})
} else {
newVars[key] = valueSlice
}
}
for key, value := range mapYaml {
valueMap := make(map[string]any)
err := yaml.Unmarshal([]byte(value), &valueMap)
if err != nil {
showErrorModal(key+" has wrong format.",
func(buttonIndex int, buttonLabel string) {
pages.SwitchToPage("Features")
})
} else {
newVars[key] = valueMap
}
}
for key, value := range newVars {
extraVars[key] = value
}
saveInventory()
flexHaMode.Clear()
initFlexHaMode()
pages.SwitchToPage("HA Mode")
})
formDown.AddButton("Back", func() {
pages.SwitchToPage("Edit Hosts")
})
formDown.AddButton("Quit", func() {
showQuitModal()
})
flexFeatures.SetDirection(tview.FlexRow).
AddItem(formFeatures, 0, 1, true).
AddItem(formDown, 3, 1, false)
}