-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
303 lines (257 loc) · 10.3 KB
/
config.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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"reflect"
"strconv"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type Config struct {
Theme *string `json:"theme"` // system, light, dark
ColorScheme *string `json:"colorScheme"` // default, midnightAsh
UseSystemTitleBar *bool `json:"useSystemTitleBar"` // true, false
EnableLogging *bool `json:"enableLogging"` // true, false
EnableTrace *bool `json:"enableTrace"` // true, false
EnableDebug *bool `json:"enableDebug"` // true, false
EnableInfo *bool `json:"enableInfo"` // true, false
EnableWarn *bool `json:"enableWarn"` // true, false
EnableError *bool `json:"enableError"` // true, false
EnableFatal *bool `json:"enableFatal"` // true, false
MaxLogFiles *int `json:"maxLogFiles"` // int
Language *string `json:"language"` // en-US, tr-TR
SaveWindowStatus *bool `json:"saveWindowStatus"` // true, false
WindowStartState *int `json:"windowStartState"` // 0 = Normal, 1 = Maximized, 2 = Minimized, 3 = Fullscreen
WindowStartPositionX *int `json:"windowStartPositionX"` // x
WindowStartPositionY *int `json:"windowStartPositionY"` // y
WindowStartSizeX *int `json:"windowStartSizeX"` // x
WindowStartSizeY *int `json:"windowStartSizeY"` // y
WindowScale *int `json:"windowScale"` // %
Opacity *int `json:"opacity"` // %
WindowEffect *int `json:"windowEffect"` // 0 = Auto, 1 = None, 2 = Mica, 3 = Acrylic, 4 = Tabbed
CheckForUpdates *bool `json:"checkForUpdates"` // true, false
LastUpdateCheck *int `json:"lastUpdateCheck"` // unix timestamp
MatchLnkByDestination *bool `json:"matchLnkByDestination"` // true, false
MatchURLByDestination *bool `json:"matchURLByDestination"` // true, false
RenameMatchedFiles *bool `json:"renameMatchedFiles"` // true, false
ChangeDescriptionOfMathcedLnkFiles *bool `json:"changeDescriptionOfMathcedLnkFiles"` // true, false
}
func GetDefaultConfig() Config {
defaultTheme := "system"
defaultColorScheme := "default"
defaultUseSystemTitleBar := false
defaultEnableLogging := true
defaultEnableTrace := false
defaultEnableDebug := false
defaultEnableInfo := true
defaultEnableWarn := true
defaultEnableError := true
defaultEnableFatal := true
defaultMaxLogFiles := 20
defaultLanguage := "en-US"
defaultSaveWindowStatus := true
defaultWindowStartState := 0
defaultWindowStartPositionX := -100000
defaultWindowStartPositionY := -100000
defaultWindowStartSizeX := -100000
defaultWindowStartSizeY := -100000
defaultWindowScale := 100
defaultOpacity := 80
defaultWindowEffect := 3
defaultCheckForUpdates := true
defaultLastUpdateCheck := 0
defaultMatchLnkByDestination := true
defaultMatchURLByDestination := true
defaultRenameMatchedFiles := false
defaultChangeDescriptionOfMathcedLnkFiles := false
return Config{
Theme: &defaultTheme,
ColorScheme: &defaultColorScheme,
UseSystemTitleBar: &defaultUseSystemTitleBar,
EnableLogging: &defaultEnableLogging,
EnableTrace: &defaultEnableTrace,
EnableDebug: &defaultEnableDebug,
EnableInfo: &defaultEnableInfo,
EnableWarn: &defaultEnableWarn,
EnableError: &defaultEnableError,
EnableFatal: &defaultEnableFatal,
MaxLogFiles: &defaultMaxLogFiles,
Language: &defaultLanguage,
SaveWindowStatus: &defaultSaveWindowStatus,
WindowStartState: &defaultWindowStartState,
WindowStartPositionX: &defaultWindowStartPositionX,
WindowStartPositionY: &defaultWindowStartPositionY,
WindowStartSizeX: &defaultWindowStartSizeX,
WindowStartSizeY: &defaultWindowStartSizeY,
WindowScale: &defaultWindowScale,
Opacity: &defaultOpacity,
WindowEffect: &defaultWindowEffect,
CheckForUpdates: &defaultCheckForUpdates,
LastUpdateCheck: &defaultLastUpdateCheck,
MatchLnkByDestination: &defaultMatchLnkByDestination,
MatchURLByDestination: &defaultMatchURLByDestination,
RenameMatchedFiles: &defaultRenameMatchedFiles,
ChangeDescriptionOfMathcedLnkFiles: &defaultChangeDescriptionOfMathcedLnkFiles,
}
}
var config Config = GetDefaultConfig()
func config_init() error {
err := CreateConfigIfNotExist()
if err != nil {
return errors.New("failed to create config file")
}
err = ReadConfig(configPath)
if err != nil {
config = GetDefaultConfig()
} else {
merge_defaults()
}
return nil
}
func merge_defaults() {
defaultConfig := GetDefaultConfig()
fmt.Println("Merging default config")
v := reflect.ValueOf(&config).Elem()
t := v.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldName := field.Name
fieldValue := v.FieldByName(fieldName)
if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
// If config's field is nil, set it to the default value's field
defaultValue := reflect.ValueOf(&defaultConfig).Elem().FieldByName(fieldName)
fieldValue.Set(defaultValue)
}
}
}
func (app *App) GetConfig() Config {
return config
}
func (app *App) GetConfigField(fieldName string) interface{} {
runtime.LogDebug(app.ctx, fmt.Sprintf("Attempting to get config field %s", fieldName))
// Get the reflection Type and Value of the Config struct
v := reflect.ValueOf(&config).Elem()
t := v.Type()
// Find the field by name
_, found := t.FieldByName(fieldName)
if !found {
runtime.LogWarning(app.ctx, fmt.Sprintf("Unknown config field: %s", fieldName))
return "undefined"
}
// Get the field value
fieldValue := v.FieldByName(fieldName)
// Check if the field is a pointer
if fieldValue.Kind() == reflect.Ptr {
if fieldValue.IsNil() {
runtime.LogWarning(app.ctx, fmt.Sprintf("Config field %s is nil", fieldName))
return "undefined"
}
// Dereference the pointer
fieldValue = fieldValue.Elem()
}
runtime.LogDebug(app.ctx, fmt.Sprintf("Config field %s has value: %v", fieldName, fieldValue.Interface()))
return fieldValue.Interface()
}
func (app *App) SetConfigField(fieldName string, value interface{}) {
runtime.LogDebug(app.ctx, fmt.Sprintf("Attempting to set config field %s to %v", fieldName, value))
v := reflect.ValueOf(&config).Elem()
t := v.Type()
_, found := t.FieldByName(fieldName)
if !found {
runtime.LogWarning(app.ctx, fmt.Sprintf("Unknown config field: %s", fieldName))
return
}
fieldValue := v.FieldByName(fieldName)
if !fieldValue.IsValid() {
runtime.LogWarning(app.ctx, fmt.Sprintf("Invalid field: %s", fieldName))
return
}
if fieldValue.Kind() == reflect.Ptr {
runtime.LogDebug(app.ctx, fmt.Sprintf("Dereferencing config field %s", fieldName))
fieldValue = fieldValue.Elem()
}
runtime.LogDebug(app.ctx, fmt.Sprintf("Config field %s type: %v", fieldName, fieldValue.Kind()))
switch fieldValue.Kind() {
case reflect.String:
strVal, ok := value.(string)
if !ok {
runtime.LogWarning(app.ctx, fmt.Sprintf("Invalid value type for string field %s: %v", fieldName, value))
return
}
fieldValue.SetString(strVal)
case reflect.Bool:
boolVal, ok := value.(bool)
if !ok {
runtime.LogWarning(app.ctx, fmt.Sprintf("Invalid value type for boolean field %s: %v", fieldName, value))
return
}
fieldValue.SetBool(boolVal)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intVal, err := strconv.Atoi(fmt.Sprintf("%v", value))
if err != nil {
runtime.LogWarning(app.ctx, fmt.Sprintf("Invalid value type for integer field %s: %v", fieldName, value))
return
}
fieldValue.SetInt(int64(intVal))
case reflect.Float32, reflect.Float64:
floatVal, ok := value.(float64)
if !ok {
runtime.LogWarning(app.ctx, fmt.Sprintf("Invalid value type for float field %s: %v", fieldName, value))
return
}
fieldValue.SetFloat(floatVal)
case reflect.Slice:
sliceVal, ok := value.([]string)
if !ok {
runtime.LogWarning(app.ctx, fmt.Sprintf("Invalid value type for slice field %s: %v", fieldName, value))
return
}
slice := reflect.ValueOf(sliceVal)
fieldValue.Set(slice)
default:
runtime.LogWarning(app.ctx, fmt.Sprintf("Unsupported field type for field %s of type %s", fieldName, fieldValue.Kind()))
return
}
runtime.LogDebug(app.ctx, fmt.Sprintf("Config field %s set to %v", fieldName, fieldValue.Interface()))
}
// Creates a default config at configPath if none exists
func CreateConfigIfNotExist() error {
configPath = get_config_path()
if _, err := os.Stat(configPath); os.IsNotExist(err) {
config = GetDefaultConfig()
}
return nil
}
// WriteConfig writes the current config to the path
func WriteConfig(path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
err = encoder.Encode(config)
if err != nil {
return err
}
return nil
}
// Read config from path
func ReadConfig(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
decoder := json.NewDecoder(file)
config = Config{}
err = decoder.Decode(&config)
if err != nil {
return err
}
return nil
}
func (app *App) ReadConfig(path string) error {
return ReadConfig(path)
}