-
Notifications
You must be signed in to change notification settings - Fork 1
/
functionality.go
276 lines (233 loc) · 6.5 KB
/
functionality.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
"github.com/getlantern/systray"
"github.com/sc7639/mysql-notifier/icon"
toast "gopkg.in/toast.v1"
yaml "gopkg.in/yaml.v2"
)
func readSettings(readSettings chan settings, mysqlInstances chan mysql) (bool, error) { // Read settings
data, err := ioutil.ReadFile(settingsPath)
if err != nil {
log.Printf("Failed to read settings: %s\n", err.Error())
return false, err
}
s := settings{}
err = yaml.Unmarshal(data, &s)
if err != nil {
log.Printf("Failed to read settings: %s\n", err.Error())
return false, err
}
mysqlInstances <- s.Mysql
readSettings <- s
return true, nil
}
func openSettings(editor string) {
if _, err := os.Stat(settingsPath); os.IsNotExist(err) { // If settings file doesn't exist, create it and add min settings
fp, err := os.OpenFile(settingsPath, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Printf("Failed to open / create settings.yml: %s\n", err.Error())
return
}
defer fp.Close()
ys, err := yaml.Marshal(minSettings)
if err != nil {
log.Printf("Failed to create yml: %s\n", err.Error())
}
_, err = fp.Write(ys)
if err != nil {
log.Printf("Failed to write to settings.yml: %s\n", err.Error())
}
}
cmd := exec.Command(editor, settingsPath)
err := cmd.Start()
if err != nil {
log.Printf("Failed to open settings: %s\n", err.Error())
} else {
log.Println("Open Settings")
}
}
func openMysqlCMD(details map[string]string) {
// Parse ip address
var host string
if _, ok := details["host"]; !ok {
host = "127.0.0.1"
} else {
host = details["host"]
}
// Parse port
var port string
if _, ok := details["port"]; !ok {
port = "3306"
} else {
port = details["port"]
}
var cmd *exec.Cmd
if passwd := details["password"]; passwd != "" { // If password is set then execute command with password
log.Println("Password")
cmd = exec.Command("cmd", "/c", "start", "mysql", "-h"+host, "-u"+details["username"], "-p"+details["password"], "-P"+port)
} else {
log.Println("No Password")
cmd = exec.Command("cmd", "/c", "start", "mysql", "-h"+host, "-u"+details["username"], "-P"+port)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
err = cmd.Start()
if err != nil {
log.Printf("Failed to open mysql command line: %s", err)
} else {
log.Println("Open mysql cmd")
}
out, _ := ioutil.ReadAll(stdout)
sterr, _ := ioutil.ReadAll(stderr)
// log.Println("cmd /c start mysql -h"+host+" -u"+details["username"], " -P"+port)
if string(out) != "" {
log.Printf("Out pipe: %s\n", out)
}
if string(sterr) != "" {
log.Printf("Err pipe: %s\n", sterr)
}
}
func updateItem(status chan bool, title string, item *systray.MenuItem, appID string) { // On check of database connection update menu item title
for live := range status {
// log.Printf("Checked %s status: %v\n", title, live)
if live {
item.SetTitle(title + " - Live")
} else {
item.SetTitle(title + " - Dead")
}
}
}
// For each db status check, check check if the the status has changed and if so
func notifications(statuses []chan bool, appID string, connections []string, duration time.Duration) {
var prevStatuses = make([]bool, len(statuses))
var changed = make([]bool, len(statuses))
for i := range prevStatuses { // Initialize prevStatuses as true
prevStatuses[i] = true
}
var notification toast.Notification
var timer *time.Timer
var timerStarted bool
sendNotification := make(chan []bool)
for i, status := range statuses {
go func(status chan bool, i int) {
for live := range status {
if !timerStarted {
timer = time.NewTimer(duration)
timerStarted = true
log.Printf("duration: %v\n", duration)
go func(sendNot chan<- []bool) {
<-timer.C
changedArr := make([]bool, len(changed))
copy(changedArr, changed)
sendNot <- changedArr
timerStarted = false
for i := range changed {
changed[i] = false
}
}(sendNotification)
}
if prevStatuses[i] != live {
changed[i] = true
prevStatuses[i] = live
}
}
}(status, i)
}
go func(sendNot <-chan []bool, appID string) {
for {
changedItems := <-sendNot
changedIndices := make(map[string][]int)
for i, changed := range changedItems {
if changed {
if prevStatuses[i] {
changedIndices["live"] = append(changedIndices["live"], i)
} else {
changedIndices["dead"] = append(changedIndices["dead"], i)
}
}
}
// Create message and title depending on how many of the connections are down / up
for typ := range changedIndices {
title := ""
message := "Failed to ping "
numType := len(changedIndices[typ])
icon := path + "\\images\\icon-red.png"
// actions := []toast.Action{}
if typ == "live" {
message = "Successfully pinged "
icon = path + "\\images\\icon.png"
}
if numType > 1 {
title = strconv.Itoa(numType) + " Connections are " + typ
title = strings.Title(title)
message += "\n"
for i := range changedIndices[typ] {
message += strings.Title(connections[i]) + ", "
}
message += " databases"
} else {
title = connections[changedIndices[typ][0]]
message += strings.Title(title) + " database"
title = strings.Title(title + " - " + typ)
// actions = []toast.Action{
// {Type: "protocol", Label: "SSH To Server", Arguments: ""},
// }
}
if runtime.GOOS == "windows" {
notification = toast.Notification{
AppID: appID,
Icon: icon,
Title: title,
Message: message,
Duration: "long",
// Actions: actions,
}
}
err := notification.Push()
if err != nil {
log.Fatalln(err)
}
}
}
}(sendNotification, appID)
}
func updateIcon(statuses []chan bool) { // On check of database connection update menu icon
var allLive = true
var i = 0
for _, status := range statuses { // For each status
go func(status chan bool) {
for live := range status { // Wait to receive information on channel
i++
// log.Printf("status: %v, i: %v, len(statuses): %v", live, i, len(statuses))
if !live {
allLive = false
}
if i == len(statuses) { // After receiving on all channels in the array
if allLive {
systray.SetIcon(icon.Green)
systray.SetTooltip("All Ok")
} else {
systray.SetIcon(icon.Red)
systray.SetTooltip("Connection Down")
allLive = true
}
i = 0
}
}
}(status)
}
}