-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
76 lines (60 loc) · 1.55 KB
/
config.go
File metadata and controls
76 lines (60 loc) · 1.55 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
package main
import (
"context"
"encoding/json"
"log"
"os"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type AppConfig struct {
ctx context.Context
ConfigPath string
ExtTerminals []Terminal
}
func NewAppConfig() *AppConfig {
return &AppConfig{}
}
func (a *AppConfig) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *AppConfig) OpenConfigFile() {
options := runtime.OpenDialogOptions{
Filters: []runtime.FileFilter{{DisplayName: "JSON", Pattern: "*.json"}},
}
// Open the dialog
filePath, err := runtime.OpenFileDialog(a.ctx, options)
if err != nil {
log.Printf("Error retrieving file path. %v", err)
return
} else if filePath == "" {
return // Return early if the user cancelled
}
a.ReadConfig(filePath)
}
func (a *AppConfig) ReadConfig(configPath string) {
var response Response
// Read the file at the provided path
data, err := os.ReadFile(configPath)
if err != nil {
response.Status = "error"
response.Message = err.Error()
log.Printf("Config read error: %v\n", response)
runtime.EventsEmit(a.ctx, "onConfigLoaded", response)
return
}
// Decode the json from the file data
err = json.Unmarshal(data, a)
if err != nil {
response.Status = "error"
response.Message = err.Error()
log.Printf("Config read error: %v\n", response)
runtime.EventsEmit(a.ctx, "onConfigLoaded", response)
return
}
a.ConfigPath = configPath
// Attach the config to the response
response.Status = "success"
response.Message = "config loaded"
response.Data = a
runtime.EventsEmit(a.ctx, "onConfigLoaded", response)
}