-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
212 lines (192 loc) · 4.69 KB
/
app.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
package main
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/energye/systray"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
githubUrl string
dbName string
dbHandler *DBHandler
processHandler *ProcessHandler
}
// NewApp creates a new App application struct
func NewApp(githubUrl, dbName string) *App {
return &App{
githubUrl: githubUrl,
dbName: dbName,
}
}
// startup is called at application startup
func (b *App) startup(ctx context.Context) {
// Perform your setup here
b.ctx = ctx
b.dbHandler = NewDBHandler(fmt.Sprintf("./%s", b.dbName), 3*time.Second)
err := b.dbHandler.Init(ctx)
if err != nil {
log.Println(err)
}
b.processHandler = NewProcessHandler(func(id int64, runStatus string) {
runtime.EventsEmit(b.ctx, "run-status", id, runStatus)
}, func(id int64, time int64, _type string, message string) {
runtime.EventsEmit(b.ctx, "run-log", id, time, _type, message)
})
b.processHandler.Start(b.ctx, b.dbHandler)
start, _ := systray.RunWithExternalLoop(func() {
systray.SetIcon(Icon)
systray.SetTitle(APP_TITLE)
systray.SetTooltip(APP_TITLE)
mOpen := systray.AddMenuItem(fmt.Sprintf("Show %s", APP_TITLE), "Show App")
mOpen.Click(func() { runtime.WindowShow(b.ctx) })
mQuit := systray.AddMenuItem("Exit", "Exit app")
mQuit.Click(func() { systray.Quit() })
systray.SetOnClick(func(menu systray.IMenu) { runtime.WindowShow(b.ctx) })
systray.SetOnRClick(func(menu systray.IMenu) { menu.ShowMenu() })
}, func() {
runtime.Quit(b.ctx)
})
start()
}
// domReady is called after the front-end dom has been loaded
func (b *App) domReady(ctx context.Context) {
// Add your action here
runtime.EventsOn(
ctx,
"event-from-js",
func(optionalData ...interface{}) {
if len(optionalData) == 0 {
return
}
key, ok := optionalData[0].(string)
if ok {
b.onEvent(key, nil)
return
}
list := optionalData[0].([]interface{})
if len(list) == 0 {
return
}
key, ok = list[0].(string)
if !ok {
return
}
if len(list) == 1 {
b.onEvent(key, nil)
return
}
b.onEvent(key, list[1])
})
}
// shutdown is called at application termination
func (b *App) shutdown(ctx context.Context) {
// Perform your teardown here
runtime.EventsOff(ctx, "event-from-js")
// err := KillProcessByName(b.clientFileName) // Kill all child processes by their name!
b.processHandler.Stop()
}
func (b *App) onEvent(key string, value interface{}) {
fmt.Println("New Event!! key:", key, "value:", value)
}
func (b *App) OpenGithub() {
OpenBrowser(b.githubUrl)
}
func (b *App) InsertProcess(p *Process) error {
id, err := b.dbHandler.InsertProcess(b.ctx, p)
if err != nil {
return err
}
if id == 0 {
return errors.New("id is 0")
}
p.Id = id
if p.Status == 1 {
b.processHandler.AddProcess(p)
}
return nil
}
func (b *App) UpdateProcess(p *Process) error {
op, err := b.dbHandler.GetProcess(b.ctx, p.Id)
if err != nil {
return err
}
err = b.dbHandler.UpdateProcess(b.ctx, p)
if err != nil {
return err
}
if op.Command != p.Command {
b.processHandler.RemoveProcess(p.Id)
p, err = b.dbHandler.GetProcess(b.ctx, p.Id)
if err != nil {
return err
}
if p.Status == 1 {
b.processHandler.AddProcess(p)
}
}
return nil
}
func (b *App) DeleteProcess(id int64) error {
b.processHandler.RemoveProcess(id)
b.processHandler.DeleteLogs(id)
return b.dbHandler.DeleteProcess(b.ctx, id)
}
func (b *App) GetProcesses() []Process {
list, err := b.dbHandler.GetProcesses(b.ctx, false)
if err != nil {
return []Process{}
}
plist := b.processHandler.processList.GetAll()
// log.Println("plist:", plist)
for i := range list {
for j := range plist {
if plist[j] != nil && list[i].Id == plist[j].Id {
list[i].RunStatus = plist[j].RunStatus
break
}
}
}
return list
}
func (b *App) RunProcess(id int64) error {
fmt.Println("run called mother fucker")
p, err := b.dbHandler.GetProcess(b.ctx, id)
if err != nil {
return err
}
p.Status = 1
err = b.dbHandler.UpdateProcess(b.ctx, p)
if err != nil {
return err
}
b.processHandler.AddProcess(p)
return nil
}
func (b *App) StopProcess(id int64) error {
p, err := b.dbHandler.GetProcess(b.ctx, id)
if err != nil {
return err
}
p.Status = 0
err = b.dbHandler.UpdateProcess(b.ctx, p)
if err != nil {
return err
}
b.processHandler.RemoveProcess(p.Id)
return nil
}
func (b *App) GetLogs(id int64) []*Log {
return b.processHandler.GetLogs(id)
}
func (b *App) ProcessesReorder(ids []int64) bool {
return b.dbHandler.UpdateProcessesOrderId(b.ctx, ids) == nil
}
func (b *App) DeleteLogs(id int64) {
b.processHandler.ClearLogs(id)
runtime.EventsEmit(b.ctx, "run-log", id, "", "clear", "")
}