-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
132 lines (107 loc) · 2.77 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
package main
import (
"fmt"
"log"
"os"
"github.com/imdario/mergo"
)
type Formats map[string]string
type Profile struct {
File string `json:"file"`
WriteMode string `json:"write_mode"`
TemplateFile string `json:"template_file"`
TemplateVars map[string]string `json:"template_vars"`
Formats Formats `json:"formats"`
KeysCase string `json:"keys_case"`
Editor string `json:"editor"`
EditorArgs []string `json:"editor_args"`
}
func getDefaultProfile() Profile {
return Profile{
File: "~/Documents/brain_dump.md",
WriteMode: "append",
Formats: Formats{
"date": "2006-01-02",
"time": "15:04:05",
"datetime": "2006-01-02 15:04:05",
},
KeysCase: "snake_case",
Editor: "$EDITOR",
}
}
type Config map[string]Profile
func (c Config) String() string {
return marshal(c)
}
func (c Config) hasProfile(name string) bool {
if name == "" {
return false
}
for currentName := range c {
if currentName == name {
return true
}
}
return false
}
func (c Config) getProfile(name string) Profile {
var err error
profile := getDefaultProfile()
if c.hasProfile(name) {
err = mergo.Merge(&profile, c[name], mergo.WithOverride)
} else if c.hasProfile("default") {
err = mergo.Merge(&profile, c["default"], mergo.WithOverride)
}
if err != nil {
log.Println("Unable to merge default and user profile. Default profile is used.")
profile = getDefaultProfile()
}
return profile
}
func getConfig() Config {
config, err := getUserConfig()
if err != nil {
log.Println("Unable to load the configuration file. Default configuration is used.")
config = getDefaultConfig()
}
configString := expandText(config.String())
err = unmarshal(configString, &config)
if err != nil {
log.Fatal(err)
}
return config
}
func getUserConfig() (Config, error) {
var appConfigFile, cwdConfigFile, envConfigFile, configFile string
cwdConfigFile = fmt.Sprintf("%s.json", APP_NAME)
envConfigFile = os.Getenv("BRAINDUMP_CONFIG_FILE")
appConfigFile = os.ExpandEnv(APP_CONFIG_FILE)
envConfigFile = os.ExpandEnv(envConfigFile)
if fileExists(cwdConfigFile) {
configFile = cwdConfigFile
} else if envConfigFile != "" {
configFile = envConfigFile
} else {
configFile = appConfigFile
}
if fileExists(configFile) {
return loadConfigFile(configFile)
}
return Config{}, fmt.Errorf("Unable to find the configuration file")
}
func getDefaultConfig() Config {
return Config{"default": getDefaultProfile()}
}
func loadConfigFile(path string) (Config, error) {
config := Config{}
path = expandText(path)
data, err := readFile(path)
if err != nil {
return config, err
}
err = unmarshal(data, &config)
if err != nil {
return config, err
}
return config, err
}