Skip to content

Commit 46a43a6

Browse files
authored
Merge pull request #54 from KiraCore/feature/configEdditor
Feature/config editor
2 parents e3c01c2 + aec55dc commit 46a43a6

File tree

4 files changed

+241
-8
lines changed

4 files changed

+241
-8
lines changed

gui/screen_config_editor.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package gui
2+
3+
import (
4+
"fyne.io/fyne/v2"
5+
"fyne.io/fyne/v2/container"
6+
"fyne.io/fyne/v2/data/binding"
7+
"fyne.io/fyne/v2/widget"
8+
"github.com/KiraCore/kensho/helper/httph"
9+
)
10+
11+
func makeCfgEditorScreen(_ fyne.Window, g *Gui) fyne.CanvasObject {
12+
appTomlTab := container.NewTabItem("app.toml", makeTextEditTab(
13+
g,
14+
func(cfg string) error {
15+
err := httph.SetAppTomlConfig(g.sshClient, cfg, 8282)
16+
if err != nil {
17+
return err
18+
}
19+
return nil
20+
},
21+
func() (string, error) {
22+
cfg, err := httph.GetAppTomlConfig(g.sshClient, 8282)
23+
if err != nil {
24+
return "", err
25+
}
26+
return cfg, nil
27+
},
28+
))
29+
configTomlTab := container.NewTabItem("config.toml", makeTextEditTab(
30+
g,
31+
func(cfg string) error {
32+
err := httph.SetConfigTomlConfig(g.sshClient, cfg, 8282)
33+
if err != nil {
34+
return err
35+
}
36+
return nil
37+
},
38+
func() (string, error) {
39+
cfg, err := httph.GetConfigTomlConfig(g.sshClient, 8282)
40+
if err != nil {
41+
return "", err
42+
}
43+
return cfg, nil
44+
},
45+
))
46+
tabsMenu := container.NewAppTabs(appTomlTab, configTomlTab)
47+
48+
return tabsMenu
49+
}
50+
51+
func makeTextEditTab(g *Gui, saveFile func(string) error, getFile func() (string, error)) fyne.CanvasObject {
52+
configBinding := binding.NewString()
53+
configEditor := widget.NewEntryWithData(configBinding)
54+
configEditor.MultiLine = true
55+
configEditor.Disable()
56+
57+
const editButtonEnabledState = "Disable editing"
58+
const editButtonDisabledState = "Enable editing"
59+
60+
editButton := widget.NewButton(editButtonDisabledState, func() {})
61+
saveButton := widget.NewButton("Save", func() {})
62+
saveButton.Disable()
63+
refreshButton := widget.NewButton("Refresh", func() {})
64+
65+
refreshFunc := func() {
66+
g.WaitDialog.ShowWaitDialog()
67+
cfg, err := getFile()
68+
if err != nil {
69+
g.WaitDialog.HideWaitDialog()
70+
g.showErrorDialog(err, binding.NewDataListener(func() {}))
71+
}
72+
configBinding.Set(cfg)
73+
74+
g.WaitDialog.HideWaitDialog()
75+
}
76+
77+
saveFunc := func() {
78+
g.WaitDialog.ShowWaitDialog()
79+
cfg, _ := configBinding.Get()
80+
err := saveFile(cfg)
81+
if err != nil {
82+
g.WaitDialog.HideWaitDialog()
83+
g.showErrorDialog(err, binding.NewDataListener(func() {}))
84+
}
85+
86+
g.WaitDialog.HideWaitDialog()
87+
refreshFunc()
88+
}
89+
90+
editFunc := func() {
91+
if configEditor.Disabled() {
92+
editButton.SetText(editButtonEnabledState)
93+
saveButton.Enable()
94+
configEditor.Enable()
95+
} else {
96+
saveButton.Disable()
97+
editButton.SetText(editButtonDisabledState)
98+
configEditor.Disable()
99+
}
100+
}
101+
102+
editButton.OnTapped = editFunc
103+
refreshButton.OnTapped = refreshFunc
104+
saveButton.OnTapped = saveFunc
105+
106+
buttonsContainer := container.NewVBox(
107+
container.NewGridWithColumns(2, editButton, saveButton),
108+
refreshButton,
109+
)
110+
refreshFunc()
111+
return container.NewBorder(nil, buttonsContainer, nil, nil, configEditor)
112+
}

gui/screen_status.go

+42-7
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func makeStatusScreen(_ fyne.Window, g *Gui) fyne.CanvasObject {
9393
}
9494
}
9595
startButton := widget.NewButton("Start", func() {})
96+
stopButton := widget.NewButton("Stop", func() {})
9697
refresh := func() {
9798
g.WaitDialog.ShowWaitDialog()
9899
checkInterxStatus()
@@ -108,7 +109,6 @@ func makeStatusScreen(_ fyne.Window, g *Gui) fyne.CanvasObject {
108109
log.Printf("CHECKS: shidaiCheck:%v sekaiCheck:%v interxCheck:%v shidaiInfra:%v sekaiInfra:%v interxInfra:%v",
109110
shidaiCheck, sekaiCheck, interxCheck, shidaiInfra, sekaiInfra, interxInfra)
110111

111-
// TODO: first maybe we should try to restart first if shidai is not running
112112
var deployButtonCheck bool
113113

114114
if !shidaiCheck {
@@ -117,25 +117,57 @@ func makeStatusScreen(_ fyne.Window, g *Gui) fyne.CanvasObject {
117117
} else {
118118
deployButtonCheck = false
119119
}
120+
121+
log.Println("enable state: ", deployButtonCheck)
120122
if !deployButtonCheck {
121123
if shidaiInfra && sekaiInfra && interxInfra && (shidaiCheck && !sekaiCheck && !interxCheck) {
122124
startButton.Enable()
125+
stopButton.Disable()
123126
log.Println("start button enabled")
127+
} else if shidaiInfra && sekaiInfra && interxInfra && (shidaiCheck && sekaiCheck && interxCheck) {
128+
stopButton.Enable()
129+
startButton.Disable()
130+
log.Println("stop button enabled")
124131
} else {
125132
startButton.Disable()
133+
stopButton.Disable()
126134
log.Println("start button disabled")
127135
}
128-
}
129-
130-
log.Println("enable state: ", deployButtonCheck)
131-
if deployButtonCheck {
132-
deployButton.Enable()
133136
} else {
134-
deployButton.Disable()
137+
deployButton.Enable()
135138
}
136139

137140
defer g.WaitDialog.HideWaitDialog()
138141
}
142+
143+
//stop button logic
144+
stopButton.OnTapped = func() {
145+
g.WaitDialog.ShowWaitDialog()
146+
var payloadStruct = types.RequestDeployPayload{
147+
Command: "stop",
148+
}
149+
payload, err := json.Marshal(payloadStruct)
150+
if err != nil {
151+
log.Println("ERROR when executing payload:", err.Error())
152+
g.WaitDialog.HideWaitDialog()
153+
g.showErrorDialog(err, binding.NewDataListener(func() {}))
154+
155+
return
156+
}
157+
out, err := httph.ExecHttpRequestBySSHTunnel(g.sshClient, types.SEKIN_EXECUTE_ENDPOINT, "POST", payload)
158+
if err != nil {
159+
log.Println("ERROR when executing payload:", err.Error())
160+
g.WaitDialog.HideWaitDialog()
161+
g.showErrorDialog(err, binding.NewDataListener(func() {}))
162+
return
163+
}
164+
log.Println("START out:", string(out))
165+
g.WaitDialog.HideWaitDialog()
166+
refresh()
167+
}
168+
stopButton.Disable()
169+
170+
//start button
139171
startButton.OnTapped = func() {
140172
g.WaitDialog.ShowWaitDialog()
141173
var payloadStruct = types.RequestDeployPayload{
@@ -144,12 +176,14 @@ func makeStatusScreen(_ fyne.Window, g *Gui) fyne.CanvasObject {
144176
payload, err := json.Marshal(payloadStruct)
145177
if err != nil {
146178
log.Println("ERROR when executing payload:", err.Error())
179+
g.WaitDialog.HideWaitDialog()
147180
g.showErrorDialog(err, binding.NewDataListener(func() {}))
148181
return
149182
}
150183
out, err := httph.ExecHttpRequestBySSHTunnel(g.sshClient, types.SEKIN_EXECUTE_ENDPOINT, "POST", payload)
151184
if err != nil {
152185
log.Println("ERROR when executing payload:", err.Error())
186+
g.WaitDialog.HideWaitDialog()
153187
g.showErrorDialog(err, binding.NewDataListener(func() {}))
154188
return
155189
}
@@ -171,6 +205,7 @@ func makeStatusScreen(_ fyne.Window, g *Gui) fyne.CanvasObject {
171205
defer refresh()
172206
return container.NewBorder(nil,
173207
container.NewVBox(startButton,
208+
stopButton,
174209
deployButton,
175210
widget.NewSeparator(),
176211
refreshButton), nil, nil,

gui/tabs.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ var (
3232
Title: "Logs",
3333
View: makeLogScreen,
3434
},
35+
"config": {
36+
Title: "Configs",
37+
View: makeCfgEditorScreen,
38+
},
3539
"test": {},
3640
}
3741

3842
TabsIndex = map[string][]string{
39-
"": {"status", "nodeInfo", "networkTree", "terminal", "logs"},
43+
"": {"status", "nodeInfo", "networkTree", "config", "terminal", "logs"},
4044
"test": {"a", "b"},
4145
}
4246
)

helper/httph/httph.go

+82
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,85 @@ func GetDashboardInfo(sshClient *ssh.Client, shidaiPort int) (*Dashboard, error)
274274
}
275275
return data, nil
276276
}
277+
278+
type ConfigRequest struct {
279+
Type string `json:"type"`
280+
TomlData string `json:"toml_data"`
281+
}
282+
283+
const (
284+
configTomlType = "config_toml"
285+
appTomlType = "app_toml"
286+
)
287+
288+
// returns app.toml as string from remote host
289+
func GetAppTomlConfig(sshClient *ssh.Client, shidaiPort int) (string, error) {
290+
url := fmt.Sprintf("http://localhost:%v/config", shidaiPort)
291+
payload := ConfigRequest{
292+
Type: appTomlType,
293+
}
294+
payloadBytes, err := json.Marshal(payload)
295+
if err != nil {
296+
return "", err
297+
}
298+
299+
out, err := ExecHttpRequestBySSHTunnel(sshClient, url, http.MethodPost, payloadBytes)
300+
if err != nil {
301+
return "", fmt.Errorf("error when getting toml config from remote\nOut: %v\nerr: %w", string(out), err)
302+
}
303+
return string(out), nil
304+
}
305+
306+
// returns config.toml as string from remote host
307+
func GetConfigTomlConfig(sshClient *ssh.Client, shidaiPort int) (string, error) {
308+
url := fmt.Sprintf("http://localhost:%v/config", shidaiPort)
309+
payload := ConfigRequest{
310+
Type: configTomlType,
311+
}
312+
payloadBytes, err := json.Marshal(payload)
313+
if err != nil {
314+
return "", err
315+
}
316+
317+
out, err := ExecHttpRequestBySSHTunnel(sshClient, url, http.MethodPost, payloadBytes)
318+
if err != nil {
319+
return "", fmt.Errorf("error when getting toml config from remote\nOut: %v\nerr: %w", string(out), err)
320+
}
321+
return string(out), nil
322+
}
323+
324+
// put
325+
func SetConfigTomlConfig(sshClient *ssh.Client, config string, shidaiPort int) error {
326+
url := fmt.Sprintf("http://localhost:%v/config", shidaiPort)
327+
payload := ConfigRequest{
328+
Type: configTomlType,
329+
TomlData: config,
330+
}
331+
payloadBytes, err := json.Marshal(payload)
332+
if err != nil {
333+
return err
334+
}
335+
336+
out, err := ExecHttpRequestBySSHTunnel(sshClient, url, http.MethodPut, payloadBytes)
337+
if err != nil {
338+
return fmt.Errorf("error when setting config.toml from remote\nOut: %v\nerr: %w", string(out), err)
339+
}
340+
return nil
341+
}
342+
func SetAppTomlConfig(sshClient *ssh.Client, config string, shidaiPort int) error {
343+
url := fmt.Sprintf("http://localhost:%v/config", shidaiPort)
344+
payload := ConfigRequest{
345+
Type: appTomlType,
346+
TomlData: config,
347+
}
348+
payloadBytes, err := json.Marshal(payload)
349+
if err != nil {
350+
return err
351+
}
352+
353+
out, err := ExecHttpRequestBySSHTunnel(sshClient, url, http.MethodPut, payloadBytes)
354+
if err != nil {
355+
return fmt.Errorf("error when setting config.toml from remote\nOut: %v\nerr: %w", string(out), err)
356+
}
357+
return nil
358+
}

0 commit comments

Comments
 (0)