-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.go
More file actions
332 lines (290 loc) · 9.09 KB
/
main.go
File metadata and controls
332 lines (290 loc) · 9.09 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"context"
_ "embed"
"fmt"
"os"
"time"
"github.com/ActiveState/cli/cmd/state-tray/internal/menu"
"github.com/ActiveState/cli/cmd/state-tray/internal/open"
"github.com/ActiveState/cli/internal/analytics"
"github.com/ActiveState/cli/internal/appinfo"
"github.com/ActiveState/cli/internal/config"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/events"
"github.com/ActiveState/cli/internal/exeutils"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/installation"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/machineid"
"github.com/ActiveState/cli/internal/osutils/autostart"
"github.com/ActiveState/cli/internal/rtutils"
"github.com/ActiveState/cli/internal/runbits/panics"
"github.com/ActiveState/cli/internal/svcmanager"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
"github.com/getlantern/systray"
"github.com/rollbar/rollbar-go"
"github.com/shirou/gopsutil/process"
"github.com/spf13/cast"
)
//go:embed icons/icon.ico
var iconFile []byte
//go:embed icons/icon-update.ico
var iconUpdateFile []byte
func main() {
verbose := os.Getenv("VERBOSE") != ""
logging.CurrentHandler().SetVerbose(verbose)
logging.SetupRollbar(constants.StateTrayRollbarToken)
systray.Run(onReady, onExit)
}
func onReady() {
var exitCode int
defer func() {
if panics.HandlePanics(recover()) {
exitCode = 1
}
logging.Debug("onReady is done with exit code %d", exitCode)
if err := events.WaitForEvents(1*time.Second, rollbar.Close, authentication.LegacyClose); err != nil {
logging.Warning("Failed to wait for rollbar to close")
}
os.Exit(exitCode)
}()
err := run()
if err != nil {
errMsg := errs.Join(err, ": ").Error()
logging.Error("Systray encountered an error: %v", errMsg)
fmt.Fprintln(os.Stderr, errMsg)
exitCode = 1
}
}
func run() (rerr error) {
cfg, err := config.New()
if err != nil {
return errs.Wrap(err, "Could not get new config instance")
}
defer rtutils.Closer(cfg.Close, &rerr)
analytics.Configure(cfg)
machineid.Configure(cfg)
machineid.SetErrorLogger(logging.Error)
running, err := isTrayRunning(cfg)
if err != nil {
return errs.Wrap(err, "Could not check for running ActiveState Desktop process")
}
if running {
return errs.New("ActiveState Desktop is already running")
}
if err := cfg.Set(installation.ConfigKeyTrayPid, os.Getpid()); err != nil {
return errs.Wrap(err, "Could not write pid to config file.")
}
systray.SetIcon(iconFile)
svcm := svcmanager.New(cfg)
if err := svcm.Start(); err != nil {
return errs.Wrap(err, "Service failed to start")
}
model, err := model.NewSvcModel(context.Background(), cfg, svcm)
if err != nil {
return errs.Wrap(err, "Could not create new service model")
}
systray.SetTooltip(locale.Tl("tray_tooltip", constants.TrayAppName))
mUpdate := systray.AddMenuItem(
locale.Tl("tray_update_title", "Update Available"),
locale.Tl("tray_update_tooltip", "Update your ActiveState Desktop installation"),
)
logging.Debug("hiding systray menu")
mUpdate.Hide()
updNotice := updateNotice{
item: mUpdate,
}
closeUpdateSupervision := superviseUpdate(model, &updNotice)
defer closeUpdateSupervision()
mAbout := systray.AddMenuItem(
locale.Tl("tray_about_title", "About State Tool"),
locale.Tl("tray_about_tooltip", "Information about the State Tool"),
)
systray.AddSeparator()
mDoc := systray.AddMenuItem(
locale.Tl("tray_documentation_title", "Documentation"),
locale.Tl("tray_documentation_tooltip", "Open State Tool Docs"),
)
mPlatform := systray.AddMenuItem(locale.Tl("tray_platform_title", "ActiveState Platform"), "")
mDashboard := mPlatform.AddSubMenuItem(
locale.Tl("tray_dashboard_title", "Dashboard"),
locale.Tl("tray_dashboard_tooltip", "Open ActiveState Platform dashboard"),
)
mLearn := mPlatform.AddSubMenuItem(
locale.Tl("tray_blog_title", "Blog"),
locale.Tl("tray_blog_tooltip", "Open ActiveState blog"),
)
mSupport := mPlatform.AddSubMenuItem(
locale.Tl("tray_support_title", "Support"),
locale.Tl("tray_support_tooltip", "Open support page"),
)
systray.AddSeparator()
trayInfo := appinfo.TrayApp()
as := autostart.New(trayInfo.Name(), trayInfo.Exec(), cfg)
enabled, err := as.IsEnabled()
if err != nil {
return errs.Wrap(err, "Could not check if app autostart is enabled")
}
mAutoStart := systray.AddMenuItemCheckbox(
locale.Tl("tray_autostart", "Start on Login"), "", enabled,
)
systray.AddSeparator()
mProjects := systray.AddMenuItem(locale.Tl("tray_projects_title", "Local Projects"), "")
mReload := mProjects.AddSubMenuItem("Reload", "Reload the local projects listing")
localProjectsUpdater := menu.NewLocalProjectsUpdater(mProjects)
localProjects, err := model.LocalProjects(context.Background())
if err != nil {
logging.Error("Could not get local projects listing: %v", err)
}
localProjectsUpdater.Update(localProjects)
systray.AddSeparator()
quit, err := setupQuit(model)
if err != nil {
return errs.Wrap(err, "Could not setup quit channel")
}
for {
select {
case <-mAbout.ClickedCh:
logging.Debug("About event")
err = open.TerminalAndWait(appinfo.StateApp().Exec() + " --version")
if err != nil {
logging.Error("Could not open command prompt: %v", err)
}
case <-mDoc.ClickedCh:
logging.Debug("Documentation event")
err = open.Browser(constants.TrayDocumentationURL)
if err != nil {
logging.Error("Could not open documentation url: %v", err)
}
case <-mLearn.ClickedCh:
logging.Debug("Learn event")
err = open.Browser(constants.ActiveStateBlogURL)
if err != nil {
logging.Error("Could not open blog url: %v", err)
}
case <-mSupport.ClickedCh:
logging.Debug("Support event")
err = open.Browser(constants.ActiveStateSupportURL)
if err != nil {
logging.Error("Could not open support url: %v", err)
}
case <-mDashboard.ClickedCh:
logging.Debug("Account event")
err = open.Browser(constants.ActiveStateDashboardURL)
if err != nil {
logging.Error("Could not open account url: %v", err)
}
case <-mReload.ClickedCh:
logging.Debug("Projects event")
localProjects, err = model.LocalProjects(context.Background())
if err != nil {
logging.Error("Could not get local projects listing: %v", err)
}
localProjectsUpdater.Update(localProjects)
case <-mAutoStart.ClickedCh:
logging.Debug("Autostart event")
var err error
enabled, err := as.IsEnabled()
if err != nil {
logging.Error("Could not check if autostart is enabled: %v", err)
}
if enabled {
logging.Debug("Disable")
err = as.Disable()
if err == nil {
mAutoStart.Uncheck()
}
} else {
logging.Debug("Enable")
err = as.Enable()
if err == nil {
mAutoStart.Check()
}
}
if err != nil {
logging.Error("Could not toggle autostart tray: %v", errs.Join(err, ": "))
}
case <-mUpdate.ClickedCh:
logging.Debug("Update event")
updlgInfo := appinfo.UpdateDialogApp()
if err := execute(updlgInfo.Exec(), nil); err != nil {
return errs.New("Could not execute: %s", updlgInfo.Name())
}
case <-quit:
logging.Debug("Quit event")
model.CloseSubscriptions()
systray.Quit()
}
}
}
func setupQuit(model *model.SvcModel) (chan struct{}, error) {
mQuit := systray.AddMenuItem(locale.Tl("tray_exit", "Exit"), "")
quitSub, err := model.Quit(context.Background())
if err != nil {
return nil, errs.Wrap(err, "Could not subscribe so service quit event")
}
quit := make(chan struct{})
go func() {
for {
select {
case <-mQuit.ClickedCh:
logging.Debug("Quit clicked")
quit <- struct{}{}
case <-quitSub:
logging.Debug("Quit from subscription")
quit <- struct{}{}
}
}
}()
return quit, nil
}
func onExit() {
logging.Debug("systray.OnExit() was called.")
cfg, err := config.New()
if err != nil {
logging.Error("Could not get configuration object on Systray exit")
return
}
defer func() {
if err := cfg.Close(); err != nil {
logging.Error("Failed to close config after exiting systray: %w", err)
}
}()
err = cfg.SetWithLock(installation.ConfigKeyTrayPid, func(setPidI interface{}) (interface{}, error) {
setPid := cast.ToInt(setPidI)
if setPid != os.Getpid() {
return nil, errs.New("PID in configuration file does not match PID of Systray shutting down")
}
return "", nil
})
if err != nil {
logging.Error("Failed to unset Systray PID in configuration file: %w", err)
}
}
func execute(exec string, args []string) error {
if !fileutils.FileExists(exec) {
return errs.New("Could not find: %s", exec)
}
if _, err := exeutils.ExecuteAndForget(exec, args); err != nil {
return errs.Wrap(err, "Could not start %s", exec)
}
return nil
}
func isTrayRunning(cfg *config.Instance) (bool, error) {
pid := cfg.GetInt(installation.ConfigKeyTrayPid)
if pid <= 0 {
return false, nil
}
pidExists, err := process.PidExists(int32(pid))
if err != nil {
return false, errs.Wrap(err, "Could not verify if pid exists")
}
if !pidExists {
return false, nil
}
return true, nil
}