-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
240 lines (209 loc) · 6.86 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
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
package main
import (
"context"
"errors"
"sync"
"time"
"github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/stnokott/r6-dissect-influx/internal/config"
"github.com/stnokott/r6-dissect-influx/internal/constants"
"github.com/stnokott/r6-dissect-influx/internal/db"
"github.com/stnokott/r6-dissect-influx/internal/update"
"github.com/stnokott/r6-dissect-influx/internal/utils"
)
// App struct
type App struct {
ctx context.Context
config *config.Config
influxClient *db.InfluxClient
influxClientMutex sync.Mutex
roundsWatcherStop context.CancelFunc
}
// NewApp creates a new instance of App with the provided config.
func NewApp(cfg *config.Config) *App {
return &App{config: cfg}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// AppInfo contains information about the app itself.
type AppInfo struct {
ProjectName string
Version string
Commit string
GithubURL string
}
// GetWindowTitle returns the window title from constants.
func (a *App) GetWindowTitle() string {
return constants.WINDOW_TITLE
}
// GetAppInfo provides information about the app itself.
func (a *App) GetAppInfo() *AppInfo {
return &AppInfo{
ProjectName: constants.ProjectName,
Version: constants.Version,
Commit: constants.Commit,
GithubURL: constants.GithubURL.String(),
}
}
// ReleaseInfo contains information about a release (of this binary).
type ReleaseInfo struct {
IsNewer bool
Version string
Commitish string
PublishedAt time.Time
Changelog string
}
func (*App) getLatestRelease() (*ReleaseInfo, error) {
release, err := update.GetLatestRelease()
if err != nil {
return nil, err
}
if len(release.Commitish) > 7 {
release.Commitish = release.Commitish[:7]
}
return &ReleaseInfo{
IsNewer: release.IsNewer(),
Version: release.SemVer.String(),
Commitish: release.Commitish,
PublishedAt: release.PublishedAt,
Changelog: release.Body,
}, nil
}
// RequestLatestReleaseInfo queries information about the latest release of the app in the background
// and emits an event with the data or an error when finished.
func (a *App) RequestLatestReleaseInfo() {
latest, err := a.getLatestRelease()
if err == nil {
runtime.EventsEmit(a.ctx, eventNames.LatestReleaseInfo, latest)
} else {
runtime.EventsEmit(a.ctx, eventNames.LatestReleaseInfoErr, err.Error())
}
}
// StartReleaseWatcher starts a background task that regularly checks for updates.
func (a *App) StartReleaseWatcher() {
ticker := time.NewTicker(constants.UpdateCheckInterval)
go func() {
for {
a.RequestLatestReleaseInfo()
<-ticker.C
}
}()
}
// StartUpdate will download the latest release
func (a *App) StartUpdate() error {
release, err := update.GetLatestRelease()
if err != nil {
return err
}
if !release.IsNewer() {
return errors.New("no update available")
}
chProgress := release.DownloadAndApply()
go func() {
for {
progressInfo, ok := <-chProgress
if !ok {
runtime.EventsEmit(a.ctx, eventNames.UpdateProgress, "Restarting...")
if err = utils.RestartSelf(); err != nil {
runtime.EventsEmit(a.ctx, eventNames.UpdateErr, err)
}
// give time for process to launch
time.Sleep(3 * time.Second)
// no further event needed as app is expected to be shut down now
return
} else if progressInfo.Err != nil {
runtime.EventsEmit(a.ctx, eventNames.UpdateErr, progressInfo.Err.Error())
return
} else {
runtime.EventsEmit(a.ctx, eventNames.UpdateProgress, progressInfo.Task)
}
}
}()
return nil
}
// OpenGameDirDialog will open a directory selector dialog to choose the R6 game directory.
func (a *App) OpenGameDirDialog() (string, error) {
options := runtime.OpenDialogOptions{
Title: "Choose game directory",
}
return runtime.OpenDirectoryDialog(a.ctx, options)
}
// AutodetectGameDir will attempt to automatically derive the game directory from a few options, i.e. the registry.
// If the directory cannot be derived, an error will be returned.
func (*App) AutodetectGameDir() (string, error) {
return config.GameFolderFromRegistry()
}
// ValidateGameDir checks whether the provided game directory string points to a valid
// installation directory.
func (*App) ValidateGameDir(v string) error {
return config.GameDirValidator.Validate(v)
}
// ValidateInfluxHost performs string-only validation on the InfluxDB host (no network IO).
func (*App) ValidateInfluxHost(v string) error {
return config.InfluxHostValidator.Validate(v)
}
// ValidateInfluxPort performs string-only validation on the InfluxDB port (no network IO).
func (*App) ValidateInfluxPort(v string) error {
return config.InfluxPortValidator.Validate(v)
}
// ValidateInfluxOrg performs string-only validation on the InfluxDB org (no network IO).
func (*App) ValidateInfluxOrg(v string) error {
return config.InfluxOrgValidator.Validate(v)
}
// ValidateInfluxBucket performs string-only validation on the InfluxDB bucket (no network IO).
func (*App) ValidateInfluxBucket(v string) error {
return config.InfluxBucketValidator.Validate(v)
}
// ValidateInfluxToken performs string-only validation on the InfluxDB token (no network IO).
func (*App) ValidateInfluxToken(v string) error {
return config.InfluxTokenValidator.Validate(v)
}
// GetConfig returns the currently persisted config.
func (a *App) GetConfig() *config.Config {
return a.config
}
// IsConfigComplete checks if all required config fields are present in the current config.
func (a *App) IsConfigComplete() bool {
return a.config.IsComplete()
}
// InfluxClientFromConfig creates a new InfluxDB client based on the provided config and tests if a writable connection can be established.
// If successful, the client will be saved for future use and the details of the connection are returned.
func (a *App) InfluxClientFromConfig(cfg *config.Config) (details *db.ConnectionDetails, err error) {
if cfg == nil || !cfg.IsComplete() {
return nil, errors.New("config incomplete, please setup first")
}
newClient := cfg.NewInfluxClient()
details, err = newClient.ValidateConn(10 * time.Second)
if err != nil {
return
}
if err = cfg.Write(); err != nil {
return
}
a.config = cfg
// close old client before assigning newClient
a.influxClientMutex.Lock()
defer a.influxClientMutex.Unlock()
if a.influxClient != nil {
a.influxClient.Close()
}
a.influxClient = newClient
go a.handleInfluxEvents(a.influxClient.LoopAsync())
return
}
// InfluxClientFromSettings uses the persisted settings to create a new InfluxDB client (see InfluxClientFromConfig).
func (a *App) InfluxClientFromSettings() (details *db.ConnectionDetails, err error) {
return a.InfluxClientFromConfig(a.config)
}
func (a *App) handleInfluxEvents(eventChan <-chan db.InfluxEvent) {
for {
event, ok := <-eventChan
if !ok {
return
}
runtime.EventsEmit(a.ctx, eventNames.RoundPush, event)
}
}