-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
188 lines (161 loc) · 4.65 KB
/
main.go
File metadata and controls
188 lines (161 loc) · 4.65 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"apps.z7.ai/usm/internal/agent"
"apps.z7.ai/usm/internal/browser"
"apps.z7.ai/usm/internal/cli"
"apps.z7.ai/usm/internal/icon"
"apps.z7.ai/usm/internal/ui"
"apps.z7.ai/usm/internal/usm"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
usmsync "apps.z7.ai/usm/internal/sync"
)
// appType detects the application type from the command line arguments and the runtime
type appType struct {
args []string
}
// IsCLI returns true if the application is a CLI app
func (a *appType) IsCLI() bool {
return len(a.args) > 1 && a.args[1] == "cli"
}
// IsGUI returns true if the application is a GUI app
func (a *appType) IsGUI() bool {
return !a.IsCLI()
}
// IsMessageFromBrowserExtension returns true if the application is a message from the browser extension
func (a *appType) IsMessageFromBrowserExtension() bool {
return len(a.args) > 1 && browser.MessageFromExtension(a.args[1:])
}
// IsMobile returns true if the application is running on a mobile device
func (a *appType) IsMobile() bool {
return runtime.GOOS == "android" || runtime.GOOS == "ios"
}
// IsWindowsOS returns true if the application is running on Windows
func (a *appType) IsWindowsOS() bool {
return runtime.GOOS == "windows"
}
func main() {
at := &appType{args: os.Args}
// handle application start: CLI, GUI
if at.IsCLI() && at.IsMobile() {
fmt.Fprintln(os.Stderr, "CLI app is unsupported on this OS")
os.Exit(1)
}
if !at.IsCLI() && at.IsWindowsOS() {
// On Windows, to ship a single binary for GUI and CLI we need to build as
// "console binary" and detach the console when running as GUI
ui.DetachConsole()
}
fyneApp := app.NewWithID(ui.AppID)
fyneApp.SetIcon(icon.USMIcon)
s, err := makeStorage(at, fyneApp)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Write the native manifests to support browser native messaging for the current OS
// TODO: this should be once at installation time
_ = browser.WriteNativeManifests()
// Handle message from browser extension
if at.IsMessageFromBrowserExtension() {
browser.HandleNativeMessage(s)
return
}
if at.IsCLI() {
// Run the CLI app
cli.Run(os.Args, s)
return
}
// Clean up any orphaned sync state from interrupted syncs
if vaults, vErr := s.Vaults(); vErr == nil {
for _, name := range vaults {
usmsync.CleanupOrphanedSync(filepath.Join(s.Root(), "storage", name))
}
}
// check for running instance looking at the health service
if ui.HealthServiceCheck(s.LockFilePath()) {
fmt.Fprintln(os.Stderr, "USM GUI is already running")
os.Exit(1)
}
// start the health service
go func() { _, _ = ui.HealthService(s.LockFilePath()) }()
// Start sync service if enabled in preferences
var syncService *usmsync.Service
appState, loadErr := s.LoadAppState()
if loadErr == nil && appState.Preferences != nil && appState.Preferences.Sync.IsEnabled() {
var svcErr error
syncService, svcErr = usmsync.NewService(usmsync.ServiceConfig{
PeerKeyPath: s.PeerKeyPath(),
TrustedPeersPath: s.TrustedPeersPath(),
StorageRoot: s.Root(),
SyncMode: appState.Preferences.Sync.Mode,
Storage: s,
})
if svcErr != nil {
log.Println("Could not create sync service:", svcErr)
}
if syncService != nil {
go func() {
if err := syncService.Start(context.Background()); err != nil {
log.Println("Could not start sync service:", err)
}
}()
}
}
// agent could be already running (e.g. from CLI)
// if not, start it
var agentType agent.Type
c, err := agent.NewClient(s.SocketAgentPath())
if err == nil {
agentType, _ = c.Type()
}
// start the GUI agent if not already running
if agentType.IsZero() {
go agent.Run(agent.NewGUI(), s.SocketAgentPath())
}
// create window and run the app
w := fyneApp.NewWindow(ui.AppTitle)
w.SetMaster()
w.Resize(fyne.NewSize(400, 600))
w.SetContent(ui.MakeApp(s, w, syncService))
// Set up graceful shutdown handler
w.SetOnClosed(func() {
// Clean up resources before exit
fyne.Do(func() {
fyneApp.Quit()
})
})
// Handle OS interrupt signals (including Cmd+Q on macOS)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
// Gracefully quit the application
fyne.Do(func() {
fyneApp.Quit()
})
}()
defer func() {
if syncService != nil {
_ = syncService.Stop()
}
}()
w.ShowAndRun()
}
// makeStorage create the storage
func makeStorage(at *appType, fyneApp fyne.App) (usm.Storage, error) {
if at.IsMobile() {
// Mobile app returns the Fyne storage
return usm.NewFyneStorage(fyneApp.Storage())
}
// Otherwise returns the OS storage
return usm.NewOSStorage()
}