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
|
package config
import (
"errors"
"os"
"os/user"
"path/filepath"
)
// loads your applications config file from
// ~/.config/<appname>/config.text
func (pb *Config) Load() error {
appname, err := GetAppname() // already configured by your application
if err != nil {
return err
}
configdir, err := getConfigDir()
if err != nil {
return err
}
filename := filepath.Join(configdir, appname+".text")
_, err = SetFilename(pb, filename)
if err != nil {
return err
}
saveMu.Lock()
defer saveMu.Unlock()
err = loadTEXT(pb, filename)
return err
}
func GetAppname() (string, error) {
if APPNAME != "" {
return APPNAME, nil
}
return "", errors.New("your application must setup config.Init()")
}
func GetUsername() string {
if Get("username") != "" {
return Get("username")
}
usr, _ := user.Current()
if usr.Username != "" {
return usr.Username
}
return "notsure" // OS Idiocracy
}
func getCacheDir() (string, error) {
if Get("cacheDir") != "" {
return Get("cacheDir"), nil
}
cacheDir, _ := os.UserCacheDir()
appname, err := GetAppname() // application should have already configured this
if err != nil {
return cacheDir, err
}
return filepath.Join(cacheDir, appname), nil
}
func getConfigDir() (string, error) {
if Get("configDir") != "" {
return Get("configDir"), nil
}
configDir, _ := os.UserConfigDir()
appname, err := GetAppname() // application should have already configured this
if err != nil {
return configDir, err
}
return filepath.Join(configDir, appname), nil
}
|