Skip to content

Commit

Permalink
Add support for GUI app to display error (netbirdio#1844)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlsmaycon authored Apr 22, 2024
1 parent a2b139d commit 580c28a
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions client/ui/client_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ func main() {

var showSettings bool
flag.BoolVar(&showSettings, "settings", false, "run settings windows")
var errorMSG string
flag.StringVar(&errorMSG, "error-msg", "", "displays a error message window")

flag.Parse()

a := app.NewWithID("NetBird")
a.SetIcon(fyne.NewStaticResource("netbird", iconDisconnectedPNG))

if errorMSG != "" {
showErrorMSG(errorMSG)
return
}

client := newServiceClient(daemonAddr, a, showSettings)
if showSettings {
a.Run()
Expand Down Expand Up @@ -209,6 +216,18 @@ func (s *serviceClient) showUIElements() {
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.
func (s *serviceClient) getSettingsForm() *widget.Form {
return &widget.Form{
Expand Down Expand Up @@ -504,41 +523,31 @@ func (s *serviceClient) onTrayReady() {
case <-s.mAdminPanel.ClickedCh:
err = open.Run(s.adminURL)
case <-s.mUp.ClickedCh:
s.mUp.Disabled()
go func() {
defer s.mUp.Enable()
err := s.menuUpClick()
if err != nil {
s.runSelfCommand("error-msg", err.Error())
return
}
}()
case <-s.mDown.ClickedCh:
s.mDown.Disable()
go func() {
defer s.mDown.Enable()
err := s.menuDownClick()
if err != nil {
s.runSelfCommand("error-msg", err.Error())
return
}
}()
case <-s.mSettings.ClickedCh:
s.mSettings.Disable()
go func() {
defer s.mSettings.Enable()
proc, err := os.Executable()
if err != nil {
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()
defer s.getSrvConfig()
s.runSelfCommand("settings", "true")
}()
case <-s.mQuit.ClickedCh:
systray.Quit()
Expand All @@ -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 {
versionString := version
if unicode.IsDigit(rune(versionString[0])) {
Expand Down

0 comments on commit 580c28a

Please sign in to comment.