-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
71 lines (59 loc) · 1.07 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
package main
import (
"os"
"path"
"gopkg.in/yaml.v3"
)
var (
ConfigFilePaths = []string{
".notepad.yml",
path.Join(getHomeDir(), ".notepad.yml"),
path.Join(getHomeDir(), "go-notepad", "notepad.yml"),
}
)
var DefaultConfig = ConfigSchema{
Font: ConfigFont{
Family: "Lucida Console",
Size: 10,
Wrap: true,
},
StatusBar: ConfigStatusBar{
Enable: false,
},
}
type (
ConfigSchema struct {
Font ConfigFont
StatusBar ConfigStatusBar
}
ConfigFont struct {
Family string
Size int64
Wrap bool
}
ConfigStatusBar struct {
Enable bool
}
)
func loadConfig(filePath string) (*ConfigSchema, error) {
// Read the YAML file
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
// Unmarshal the YAML file into the Config struct
var config ConfigSchema
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
return &config, nil
}
func searchAndLoadConfig() (*ConfigSchema, error) {
for _, c := range ConfigFilePaths {
if fileExist(c) {
return loadConfig(c)
}
}
return &DefaultConfig, nil
}