Add support for GUI app to display error (#1844)

This commit is contained in:
Maycon Santos 2024-04-22 11:57:38 +02:00 committed by GitHub
parent a80c8b0176
commit 4c5e987e02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -58,12 +58,19 @@ func main() {
var showSettings bool var showSettings bool
flag.BoolVar(&showSettings, "settings", false, "run settings windows") flag.BoolVar(&showSettings, "settings", false, "run settings windows")
var errorMSG string
flag.StringVar(&errorMSG, "error-msg", "", "displays a error message window")
flag.Parse() flag.Parse()
a := app.NewWithID("NetBird") a := app.NewWithID("NetBird")
a.SetIcon(fyne.NewStaticResource("netbird", iconDisconnectedPNG)) a.SetIcon(fyne.NewStaticResource("netbird", iconDisconnectedPNG))
if errorMSG != "" {
showErrorMSG(errorMSG)
return
}
client := newServiceClient(daemonAddr, a, showSettings) client := newServiceClient(daemonAddr, a, showSettings)
if showSettings { if showSettings {
a.Run() a.Run()
@ -209,6 +216,18 @@ func (s *serviceClient) showUIElements() {
s.wSettings.Show() s.wSettings.Show()
} }
// showErrorMSG opens a fyne app window to display the supplied message
func showErrorMSG(msg string) {
app := app.New()
w := app.NewWindow("NetBird Error")
content := widget.NewLabel(msg)
content.Wrapping = fyne.TextWrapWord
w.SetContent(content)
w.Resize(fyne.NewSize(400, 100))
w.Show()
app.Run()
}
// getSettingsForm to embed it into settings window. // getSettingsForm to embed it into settings window.
func (s *serviceClient) getSettingsForm() *widget.Form { func (s *serviceClient) getSettingsForm() *widget.Form {
return &widget.Form{ return &widget.Form{
@ -504,16 +523,22 @@ func (s *serviceClient) onTrayReady() {
case <-s.mAdminPanel.ClickedCh: case <-s.mAdminPanel.ClickedCh:
err = open.Run(s.adminURL) err = open.Run(s.adminURL)
case <-s.mUp.ClickedCh: case <-s.mUp.ClickedCh:
s.mUp.Disabled()
go func() { go func() {
defer s.mUp.Enable()
err := s.menuUpClick() err := s.menuUpClick()
if err != nil { if err != nil {
s.runSelfCommand("error-msg", err.Error())
return return
} }
}() }()
case <-s.mDown.ClickedCh: case <-s.mDown.ClickedCh:
s.mDown.Disable()
go func() { go func() {
defer s.mDown.Enable()
err := s.menuDownClick() err := s.menuDownClick()
if err != nil { if err != nil {
s.runSelfCommand("error-msg", err.Error())
return return
} }
}() }()
@ -521,24 +546,8 @@ func (s *serviceClient) onTrayReady() {
s.mSettings.Disable() s.mSettings.Disable()
go func() { go func() {
defer s.mSettings.Enable() defer s.mSettings.Enable()
proc, err := os.Executable() defer s.getSrvConfig()
if err != nil { s.runSelfCommand("settings", "true")
log.Errorf("show settings: %v", err)
return
}
cmd := exec.Command(proc, "--settings=true")
out, err := cmd.CombinedOutput()
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
log.Errorf("start settings UI: %v, %s", err, string(out))
return
}
if len(out) != 0 {
log.Info("settings change:", string(out))
}
// update config in systray when settings windows closed
s.getSrvConfig()
}() }()
case <-s.mQuit.ClickedCh: case <-s.mQuit.ClickedCh:
systray.Quit() systray.Quit()
@ -556,6 +565,24 @@ func (s *serviceClient) onTrayReady() {
}() }()
} }
func (s *serviceClient) runSelfCommand(command, arg string) {
proc, err := os.Executable()
if err != nil {
log.Errorf("show %s failed with error: %v", command, err)
return
}
cmd := exec.Command(proc, fmt.Sprintf("--%s=%s", command, arg))
out, err := cmd.CombinedOutput()
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
log.Errorf("start %s UI: %v, %s", command, err, string(out))
return
}
if len(out) != 0 {
log.Infof("command %s executed: %s", command, string(out))
}
}
func normalizedVersion(version string) string { func normalizedVersion(version string) string {
versionString := version versionString := version
if unicode.IsDigit(rune(versionString[0])) { if unicode.IsDigit(rune(versionString[0])) {