forked from shibukawa/configdir
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
160 lines (142 loc) · 3.75 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// configdir provides access to configuration folder in each platforms.
//
// System wide configuration folders:
//
// - Windows: %PROGRAMDATA% (C:\ProgramData)
// - Linux/BSDs: ${XDG_CONFIG_DIRS} (/etc/xdg)
// - MacOSX: "/Library/Application Support"
//
// User wide configuration folders:
//
// - Windows: %APPDATA% (C:\Users\<User>\AppData\Roaming)
// - Linux/BSDs: ${XDG_CONFIG_HOME} (${HOME}/.config)
// - MacOSX: "${HOME}/Library/Application Support"
//
// User wide cache folders:
//
// - Windows: %LOCALAPPDATA% (C:\Users\<User>\AppData\Local)
// - Linux/BSDs: ${XDG_CACHE_HOME} (${HOME}/.cache)
// - MacOSX: "${HOME}/Library/Caches"
//
// configdir returns paths inside the above folders.
package configdir
import (
"io/ioutil"
"os"
"path/filepath"
)
type ConfigType int
const (
System ConfigType = iota
Global
All
Existing
Local
Cache
)
// Config represents each folder
type Config struct {
Path string
Type ConfigType
}
func (c Config) Open(fileName string) (*os.File, error) {
return os.Open(filepath.Join(c.Path, fileName))
}
func (c Config) Create(fileName string) (*os.File, error) {
err := c.CreateParentDir(fileName)
if err != nil {
return nil, err
}
return os.Create(filepath.Join(c.Path, fileName))
}
func (c Config) ReadFile(fileName string) ([]byte, error) {
return ioutil.ReadFile(filepath.Join(c.Path, fileName))
}
// CreateParentDir creates the parent directory of fileName inside c. fileName
// is a relative path inside c, containing zero or more path separators.
func (c Config) CreateParentDir(fileName string) error {
return os.MkdirAll(filepath.Dir(filepath.Join(c.Path, fileName)), 0755)
}
func (c Config) WriteFile(fileName string, data []byte) error {
err := c.CreateParentDir(fileName)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(c.Path, fileName), data, 0644)
}
func (c Config) MkdirAll() error {
return os.MkdirAll(c.Path, 0755)
}
func (c Config) Exists(fileName string) bool {
_, err := os.Stat(filepath.Join(c.Path, fileName))
return !os.IsNotExist(err)
}
// ConfigDir keeps setting for querying folders.
type ConfigDir struct {
VendorName string
ApplicationName string
LocalPath string
}
func New(vendorName, applicationName string) ConfigDir {
return ConfigDir{
VendorName: vendorName,
ApplicationName: applicationName,
}
}
func (c ConfigDir) joinPath(root string) string {
if c.VendorName != "" && hasVendorName {
return filepath.Join(root, c.VendorName, c.ApplicationName)
}
return filepath.Join(root, c.ApplicationName)
}
func (c ConfigDir) QueryFolders(configType ConfigType) []*Config {
if configType == Cache {
return []*Config{c.QueryCacheFolder()}
}
var result []*Config
if c.LocalPath != "" && configType != System && configType != Global {
result = append(result, &Config{
Path: c.LocalPath,
Type: Local,
})
}
if configType != System && configType != Local {
result = append(result, &Config{
Path: c.joinPath(globalSettingFolder),
Type: Global,
})
}
if configType != Global && configType != Local {
for _, root := range systemSettingFolders {
result = append(result, &Config{
Path: c.joinPath(root),
Type: System,
})
}
}
if configType != Existing {
return result
}
var existing []*Config
for _, entry := range result {
if _, err := os.Stat(entry.Path); !os.IsNotExist(err) {
existing = append(existing, entry)
}
}
return existing
}
func (c ConfigDir) QueryFolderContainsFile(fileName string) *Config {
configs := c.QueryFolders(Existing)
for _, config := range configs {
if _, err := os.Stat(filepath.Join(config.Path, fileName)); !os.IsNotExist(err) {
return config
}
}
return nil
}
func (c ConfigDir) QueryCacheFolder() *Config {
return &Config{
Path: c.joinPath(cacheFolder),
Type: Cache,
}
}