-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (88 loc) · 2.13 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
package main
import (
"io/ioutil"
"os"
"time"
"golang.org/x/sys/windows/svc"
"github.com/getlantern/systray"
)
const (
textServicesNotRunning = "There's services that aren't running"
iconServicesNotRunning = "assets/services-nok.ico"
textAllServicesRunning = "All services are running"
iconAllServicesRunning = "assets/services-ok.ico"
scanServicesEvery = 5 * time.Second
)
func main() {
systray.Run(onReady, onExit)
}
func onExit() {
}
func onReady() {
configuration, err := ReadFile()
if err != nil {
println(err.Error())
os.Exit(9)
}
services := configuration.Services
mQuit := systray.AddMenuItem("Quit", "Quit")
go monitorServices(services)
go monitorSystrayMenu(mQuit)
}
func verifyIfAllServicesAreRunning(services []string) (bool, error) {
for _, serviceName := range services {
state, err := GetServiceState(serviceName)
if err != nil {
println(err.Error())
return false, err
} else if state != svc.Running {
return false, nil
}
}
return true, nil
}
func monitorServices(services []string) {
allServicesAreRunning := false
setIconAndTitleNotOk(textServicesNotRunning)
for {
allRunning, err := verifyIfAllServicesAreRunning(services)
if err != nil {
setIconAndTitleNotOk("Error: " + err.Error() + "\nVerify if it's running with administrator privileges!")
} else if allServicesAreRunning != allRunning {
allServicesAreRunning = allRunning
if allServicesAreRunning {
setIconAndTitleOk()
} else {
setIconAndTitleNotOk(textServicesNotRunning)
}
}
time.Sleep(scanServicesEvery)
}
}
func setIconAndTitleNotOk(text string) {
systray.SetIcon(getIcon(iconServicesNotRunning))
systray.SetTitle(text)
systray.SetTooltip(text)
}
func setIconAndTitleOk() {
systray.SetIcon(getIcon(iconAllServicesRunning))
systray.SetTitle(textAllServicesRunning)
systray.SetTooltip(textAllServicesRunning)
}
func getIcon(s string) []byte {
b, err := ioutil.ReadFile(s)
if err != nil {
println(err.Error())
}
return b
}
func monitorSystrayMenu(mQuit *systray.MenuItem) {
for {
time.Sleep(100 * time.Millisecond)
select {
case <-mQuit.ClickedCh:
systray.Quit()
return
}
}
}