This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tui.go
198 lines (181 loc) · 5.5 KB
/
tui.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"fmt"
"github.com/AtlasInsideCorp/UTMStackInstaller/utils"
"github.com/rivo/tview"
)
const (
actionPageIndex = "0"
masterPageIndex = "1"
probePageIndex = "2"
uninstallPageIndex = "3"
errTag = "ERROR: "
logFileAnnouncement = "Please see the logs in /var/log/utm-setup.log for more details."
)
func tui() {
app := tview.NewApplication()
pages := tview.NewPages()
pages.AddPage(actionPageIndex, actionPage(pages, app), true, true)
pages.AddPage(masterPageIndex, masterPage(pages, app), true, false)
pages.AddPage(probePageIndex, probePage(pages, app), true, false)
pages.AddPage(uninstallPageIndex, uninstallPage(pages, app), false, false)
if err := app.SetRoot(pages, true).EnableMouse(true).Run(); err != nil {
panic(err)
}
}
func center(width, height int, p tview.Primitive) tview.Primitive {
return tview.NewFlex().
AddItem(nil, 0, 1, false).
AddItem(tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(p, height, 1, true).
AddItem(nil, 0, 1, false), width, 1, true).
AddItem(nil, 0, 1, false)
}
func alert(pages *tview.Pages, message string) *tview.Pages {
id := "alert-dialog"
return pages.AddPage(
id,
tview.NewModal().
SetText(message).
AddButtons([]string{"Accept"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
pages.HidePage(id).RemovePage(id)
}),
false,
true,
)
}
func showResults(pages *tview.Pages, app *tview.Application, message string) *tview.Pages {
id := "results-dialog"
return pages.AddPage(
id,
tview.NewModal().
SetText(message).
AddButtons([]string{"Quit"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
app.Stop()
}),
false,
true,
)
}
func actionPage(pages *tview.Pages, app *tview.Application) tview.Primitive {
list := tview.NewList().
AddItem("Install Master", "", 'a', func() {
pages.SwitchToPage(masterPageIndex)
}).
AddItem("Install Probe", "", 'b', func() {
pages.SwitchToPage(probePageIndex)
}).
AddItem("Remove UTMStack", "", 'c', func() {
pages.SwitchToPage(uninstallPageIndex)
}).
AddItem("Quit", "", 'q', func() {
app.Stop()
})
list.SetBorder(true).SetTitle("UTMStack - Select Operation").SetTitleAlign(tview.AlignCenter).SetBorderPadding(1, 0, 1, 0)
return center(33, 12, list)
}
func uninstallPage(pages *tview.Pages, app *tview.Application) tview.Primitive {
return tview.NewModal().SetText("Are you sure you want to remove UTMStack?").
AddButtons([]string{"Remove", "Cancel"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonIndex == 0 {
pages.AddPage(
"uninstalling-dialog",
tview.NewModal().SetText("Removing... This may take several minutes. Please wait."),
false,
true,
)
pages.HidePage(uninstallPageIndex)
go func() {
err := utils.Uninstall("ui")
var msg string
if err != nil {
msg = errTag + err.Error() + ". " + logFileAnnouncement
} else {
msg = "Successfully removed. " + logFileAnnouncement
}
showResults(pages, app, msg)
app.Draw()
}()
} else {
pages.SwitchToPage(actionPageIndex)
}
})
}
func masterPage(pages *tview.Pages, app *tview.Application) tview.Primitive {
form := tview.NewForm()
form.AddButton("Back", func() {
pages.SwitchToPage(actionPageIndex)
}).AddButton("Install", func() {
datadir := "/utmstack"
dbPass := utils.GenerateSecret()
pages.AddPage(
"installing-dialog",
tview.NewModal().SetText("Installing... This may take several minutes, please wait."),
false,
true,
)
pages.HidePage(masterPageIndex)
go func() {
err := utils.InstallMaster("ui", datadir, dbPass, tag, lite)
var msg string
if err != nil {
msg = errTag + err.Error() + ". " + logFileAnnouncement
} else {
msg = fmt.Sprintf("Successfully installed. Master password: %s", dbPass)
}
showResults(pages, app, msg)
app.Draw()
}()
}).AddButton("Quit", func() {
app.Stop()
})
form.SetBorder(true).SetTitle("Install Master").SetTitleAlign(tview.AlignCenter)
return center(46, 15, form)
}
func probePage(pages *tview.Pages, app *tview.Application) tview.Primitive {
form := tview.NewForm().
AddPasswordField("Master password", "", 25, '*', nil).
AddPasswordField("Confirm master password", "", 25, '*', nil).
AddInputField("Master IP or FQDN", "", 25, nil, nil)
form.AddButton("Back", func() {
pages.SwitchToPage(actionPageIndex)
}).AddButton("Install", func() {
datadir := "/utmstack"
dbPass := form.GetFormItem(0).(*tview.InputField).GetText()
confirmDBPass := form.GetFormItem(1).(*tview.InputField).GetText()
host := form.GetFormItem(2).(*tview.InputField).GetText()
if dbPass == "" || confirmDBPass == "" || host == "" {
alert(pages, "You must provide all requested data.")
} else if dbPass != confirmDBPass {
alert(pages, "Password confirmation does not match")
} else {
pages.AddPage(
"installing-dialog",
tview.NewModal().SetText("Installing... This may take several minutes. Please wait."),
false,
true,
)
pages.HidePage(probePageIndex)
go func() {
err := utils.InstallProbe("ui", datadir, dbPass, host, tag, lite)
var msg string
if err != nil {
msg = errTag + err.Error() + ". " + logFileAnnouncement
} else {
msg = "Successfully installed. " + logFileAnnouncement
}
showResults(pages, app, msg)
app.Draw()
}()
}
}).AddButton("Quit", func() {
app.Stop()
})
form.SetBorder(true).SetTitle("Install Probe").SetTitleAlign(tview.AlignCenter)
return center(46, 11, form)
}