-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (111 loc) · 2.88 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"runtime"
"github.com/energye/systray"
"github.com/isaqueveras/zoity/assets"
"github.com/isaqueveras/zoity/types"
)
func main() {
systray.Run(setup, func() {
log.Println("Closing Zoity Application")
})
}
func setup() {
systray.SetIcon(assets.Icon)
systray.SetTitle("Zoity")
systray.SetTooltip("Zoity")
createItemsInMenu()
}
func stopAllServices() {
for _, service := range types.Services {
if service.Process == nil {
continue
}
service.Kill()
}
}
func startService(service *types.Service) {
service.Kill()
command := exec.Command("bash", "-c", fmt.Sprintf(
"%scd %s && %s", service.GetEnv(), service.Path, service.Command,
))
command.Stdout, command.Stderr = os.Stdout, os.Stderr
if err := command.Start(); err != nil {
fmt.Printf("[ERROR] Error starting service %s: %v\n", service.Name, err)
return
}
service.Process = command.Process
types.TotalServiceRunning++
systray.SetIcon(assets.IconActived)
log.Println("Starting the service", service.Process.Pid, service.Name, service.Ports)
}
func createItemsInMenu() {
for idx := range types.Services {
createItemService(idx, &types.Services)
}
systray.AddSeparator()
settings := systray.AddMenuItem("Settings", "settings")
settings.AddSubMenuItem("Reload", "reload settings").Click(reloadSettings)
settings.AddSubMenuItem("Open", "open settings file").Click(openSettingsFile)
systray.AddMenuItem("Exit", "exit").Click(exit)
}
func reloadSettings() {
stopAllServices()
systray.ResetMenu()
systray.SetIcon(assets.Icon)
types.InitConfig()
createItemsInMenu()
}
func exit() {
stopAllServices()
systray.Quit()
}
func createItemService(idx int, services *[]types.Service) {
if services == nil || idx < 0 || idx >= len(*services) {
fmt.Println("[ERROR] Invalid index or null slice")
return
}
service := (*services)[idx]
item := systray.AddMenuItemCheckbox(service.Name, service.Name, false)
startItem := item.AddSubMenuItem("Start", "start the service")
stopItem := item.AddSubMenuItem("Stop", "stop the service")
stopItem.Hide()
startItem.Click(func() {
startService(&types.Services[idx])
item.Check()
stopItem.Show()
startItem.Hide()
})
stopItem.Click(func() {
service.Kill()
item.Uncheck()
stopItem.Hide()
startItem.Show()
types.TotalServiceRunning--
if types.TotalServiceRunning == 0 {
systray.SetIcon(assets.Icon)
}
log.Println("Stoping service", service.Name)
types.Services[idx].Process = nil
})
}
func openSettingsFile() {
var cmd *exec.Cmd
switch runtime.GOOS {
case "linux":
cmd = exec.Command("xdg-open", types.ConfigFile)
case "darwin":
cmd = exec.Command("open", types.ConfigFile)
case "windows":
cmd = exec.Command("cmd", "/c", "start", "", types.ConfigFile)
default:
log.Printf("unsupported operating system: %s\n", runtime.GOOS)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
_ = cmd.Run()
}