-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
config.go
118 lines (103 loc) · 3.13 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
package main
import (
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/adrg/xdg"
"github.com/caarlos0/env/v6"
"gopkg.in/yaml.v3"
)
// Config holds the configuration options for the application.
//
// At the moment, it is quite limited, only supporting the home folder and the
// file name of the metadata.
type Config struct {
Home string `env:"NAP_HOME" yaml:"home"`
File string `env:"NAP_FILE" yaml:"file"`
DefaultLanguage string `env:"NAP_DEFAULT_LANGUAGE" yaml:"default_language"`
Theme string `env:"NAP_THEME" yaml:"theme"`
PrimaryColor string `env:"NAP_PRIMARY_COLOR" yaml:"primary_color"`
PrimaryColorSubdued string `env:"NAP_PRIMARY_COLOR_SUBDUED" yaml:"primary_color_subdued"`
BrightGreenColor string `env:"NAP_BRIGHT_GREEN" yaml:"bright_green"`
GreenColor string `env:"NAP_GREEN" yaml:"green"`
BrightRedColor string `env:"NAP_BRIGHT_RED" yaml:"bright_red"`
RedColor string `env:"NAP_RED" yaml:"red"`
ForegroundColor string `env:"NAP_FOREGROUND" yaml:"foreground"`
BackgroundColor string `env:"NAP_BACKGROUND" yaml:"background"`
GrayColor string `env:"NAP_GRAY" yaml:"gray"`
BlackColor string `env:"NAP_BLACK" yaml:"black"`
WhiteColor string `env:"NAP_WHITE" yaml:"white"`
}
func newConfig() Config {
return Config{
Home: defaultHome(),
File: "snippets.json",
DefaultLanguage: defaultLanguage,
Theme: "dracula",
PrimaryColor: "#AFBEE1",
PrimaryColorSubdued: "#64708D",
BrightGreenColor: "#BCE1AF",
GreenColor: "#527251",
BrightRedColor: "#E49393",
RedColor: "#A46060",
ForegroundColor: "15",
BackgroundColor: "235",
GrayColor: "241",
BlackColor: "#373b41",
WhiteColor: "#FFFFFF",
}
}
// default helpers for the configuration.
// We use $XDG_DATA_HOME to avoid cluttering the user's home directory.
func defaultHome() string { return filepath.Join(xdg.DataHome, "nap") }
// defaultConfig returns the default config path
func defaultConfig() string {
if c := os.Getenv("NAP_CONFIG"); c != "" {
return c
}
cfgPath, err := xdg.ConfigFile("nap/config.yaml")
if err != nil {
return "config.yaml"
}
return cfgPath
}
// readConfig returns a configuration read from the environment.
func readConfig() Config {
config := newConfig()
fi, err := os.Open(defaultConfig())
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return newConfig()
}
if fi != nil {
defer fi.Close()
if err := yaml.NewDecoder(fi).Decode(&config); err != nil {
return newConfig()
}
}
if err := env.Parse(&config); err != nil {
return newConfig()
}
if strings.HasPrefix(config.Home, "~") {
home, err := os.UserHomeDir()
if err == nil {
config.Home = filepath.Join(home, config.Home[1:])
}
}
return config
}
// writeConfig returns a configuration read from the environment.
func (config Config) writeConfig() error {
fi, err := os.Create(defaultConfig())
if err != nil {
return err
}
if fi != nil {
defer fi.Close()
if err := yaml.NewEncoder(fi).Encode(&config); err != nil {
return err
}
}
return nil
}