Repository navigation
Expand file tree
/
Copy pathconfig_apply.go
More file actions
181 lines (160 loc) · 5.01 KB
/
Copy pathconfig_apply.go
File metadata and controls
181 lines (160 loc) · 5.01 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
package main
import (
"fmt"
"prochub/internal/config"
"prochub/internal/process"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// ConfigEvent is the payload broadcast to the frontend whenever the persisted
// configuration changes. It carries the full configuration so the UI has a
// single, authoritative source of truth regardless of who made the change
// (GUI, CLI, or tray).
type ConfigEvent struct {
Config config.AppConfig `json:"config"`
// Origin tells the UI where the change came from so it can avoid redundant
// work (e.g. "gui", "cli", "tray").
Origin string `json:"origin"`
}
// Config origins used in ConfigEvent.Origin.
const (
originGUI = "gui"
originCLI = "cli"
originBoot = "boot"
)
// applyConfig is the single write path for application configuration. It
// persists the configuration, applies OS-level side effects (auto-start),
// refreshes the tray language and broadcasts the full configuration to the
// frontend over the "config:changed" event.
//
// The origin argument is only used for the broadcast payload; it does not
// change the behaviour of the write.
func (a *App) applyConfig(cfg config.AppConfig, origin string) error {
a.configMu.Lock()
defer a.configMu.Unlock()
return a.applyConfigLocked(cfg, origin)
}
// applyConfigLocked is applyConfig for callers that already hold configMu. It
// keeps the lock ordering identical for every write path.
func (a *App) applyConfigLocked(cfg config.AppConfig, origin string) error {
old := a.config
if cfg.AutoStart != old.AutoStart {
if cfg.AutoStart {
if err := a.autoStartMgr.Enable(); err != nil {
return err
}
} else if err := a.autoStartMgr.Disable(); err != nil {
return err
}
}
normalizeConfig(&cfg)
a.config = cfg
if err := a.store.Save(a.config); err != nil {
a.LogSystemError("applyConfig", fmt.Sprintf("Failed to save config: %v", err))
return err
}
// Tray language depends on the locale and is not driven by the frontend.
if old.Locale != cfg.Locale {
UpdateTrayLanguage()
}
if !configEqual(old, cfg) {
if origin == "" {
origin = originGUI
}
a.emit("config:changed", ConfigEvent{Config: cfg, Origin: origin})
}
return nil
}
// normalizeConfig clamps the fields that have a restricted value set so an
// invalid value never reaches persistence.
func normalizeConfig(cfg *config.AppConfig) {
if cfg.Theme != "dark" {
cfg.Theme = "light"
}
if cfg.Locale != "en" {
cfg.Locale = "zh"
}
}
// configEqual reports whether two configurations are identical. It is used to
// skip broadcasting when a write did not actually change anything.
func configEqual(a, b config.AppConfig) bool {
if a.Locale != b.Locale ||
a.Theme != b.Theme ||
a.AutoStart != b.AutoStart ||
a.AutoStartWSL != b.AutoStartWSL ||
a.LogDir != b.LogDir ||
a.MaxLogLines != b.MaxLogLines ||
a.MaxLogFiles != b.MaxLogFiles ||
a.MaxRestart != b.MaxRestart ||
a.RestartPolicy != b.RestartPolicy {
return false
}
if len(a.Processes) != len(b.Processes) {
return false
}
for i := range a.Processes {
if !definitionEqual(a.Processes[i], b.Processes[i]) {
return false
}
}
return true
}
func definitionEqual(a, b process.Definition) bool {
if a.ID != b.ID ||
a.Name != b.Name ||
a.Command != b.Command ||
a.WorkingDir != b.WorkingDir ||
a.AutoStart != b.AutoStart ||
a.AutoRestart != b.AutoRestart ||
a.RestartPolicy != b.RestartPolicy ||
a.MaxRetries != b.MaxRetries {
return false
}
if len(a.Args) != len(b.Args) {
return false
}
for i := range a.Args {
if a.Args[i] != b.Args[i] {
return false
}
}
if len(a.Env) != len(b.Env) {
return false
}
for k, v := range a.Env {
if b.Env[k] != v {
return false
}
}
return true
}
// patchConfig applies a series of mutations to the current configuration while
// holding the config lock across the whole read-modify-write, then persists the
// result. Callers use it instead of reading a.config, mutating a copy and
// calling applyConfig, which would race with concurrent writers.
func (a *App) patchConfig(mutate func(cfg *config.AppConfig), origin string) error {
// Hold configMu across the whole read-modify-write. Releasing it between
// the read and the write would let two concurrent patches start from the
// same snapshot and silently drop one of the changes.
a.configMu.Lock()
defer a.configMu.Unlock()
cfg := a.config
mutate(&cfg)
return a.applyConfigLocked(cfg, origin)
}
// SetTheme persists the UI theme (light/dark) and broadcasts the change.
func (a *App) SetTheme(theme string) error {
defer a.recoverPanic("SetTheme")
return a.patchConfig(func(cfg *config.AppConfig) { cfg.Theme = theme }, originGUI)
}
// SetLocale persists the UI locale (zh/en), refreshing the tray language.
func (a *App) SetLocale(locale string) error {
defer a.recoverPanic("SetLocale")
return a.patchConfig(func(cfg *config.AppConfig) { cfg.Locale = locale }, originGUI)
}
// emit sends a Wails event to the frontend when the app context is ready.
func (a *App) emit(name string, data interface{}) {
if a.ctx == nil {
return
}
runtime.EventsEmit(a.ctx, name, data)
}