-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_api.go
164 lines (130 loc) · 3.48 KB
/
config_api.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
161
162
163
164
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
ini "gopkg.in/ini.v1"
)
type ConfigClient interface {
GetAll() (map[string]string, error)
Unset(system bool, key string) error
Set(system bool, key, value string) error
Get(key string) (string, error)
}
type ConfigGetter interface {
Get(key string) (string, error)
}
type RealConfigClient struct {
systemConfig string
userConfig string
}
func (rcc *RealConfigClient) Set(system bool, key, value string) error {
var configPath string
if system {
configPath = rcc.systemConfig
} else {
configPath = rcc.userConfig
}
cfg, err := ini.LooseLoad(configPath)
if err != nil {
return fmt.Errorf("error: %v", err)
}
section, k := splitKey(key)
_, err = cfg.Section(section).NewKey(k, value)
if err != nil {
return fmt.Errorf("unable to set new key: %v", err)
}
err = cfg.SaveToIndent(configPath, " ")
if err != nil {
return fmt.Errorf("unable to save: %v", err)
}
return nil
}
func (rcc *RealConfigClient) Unset(system bool, key string) error {
var configPath string
if system {
configPath = rcc.systemConfig
} else {
configPath = rcc.userConfig
}
cfg, err := ini.LooseLoad(configPath)
if err != nil {
return fmt.Errorf("error: %v", err)
}
section, k := splitKey(key)
cfg.Section(section).DeleteKey(k)
if len(cfg.Section(section).Keys()) == 0 {
cfg.DeleteSection(section)
}
err = cfg.SaveToIndent(configPath, " ")
if err != nil {
return fmt.Errorf("unable to save: %v", err)
}
return nil
}
func (rcc *RealConfigClient) Get(key string) (string, error) {
cfg, err := ini.LooseLoad(rcc.systemConfig, rcc.userConfig)
if err != nil {
return "", fmt.Errorf("failure to load config: %v", err)
}
section, k := splitKey(key)
ck := cfg.Section(section).Key(k)
return ck.Value(), nil
}
func (rcc *RealConfigClient) GetAll() (map[string]string, error) {
all := make(map[string]string)
cfg, err := ini.LooseLoad(rcc.systemConfig, rcc.userConfig)
if err != nil {
return all, fmt.Errorf("failure to load config: %v", err)
}
for _, section := range cfg.Sections() {
if len(section.Keys()) > 0 {
for _, key := range section.Keys() {
all[fmt.Sprintf("%s.%s", section.Name(), key.Name())] = key.Value()
}
}
}
return all, nil
}
func splitKey(key string) (string, string) {
parts := strings.SplitN(key, ".", 2)
return parts[0], parts[1]
}
func NewDefaultConfigClient(system System) (*RealConfigClient, error) {
var baseDir string
if xdgConfigHome := system.Getenv("XDG_CONFIG_HOME"); len(xdgConfigHome) > 0 {
baseDir = filepath.Join(xdgConfigHome, "holen")
} else {
var home string
if home = system.Getenv("HOME"); len(home) == 0 {
return nil, fmt.Errorf("$HOME environment variable not found")
}
baseDir = filepath.Join(home, ".config", "holen")
os.MkdirAll(baseDir, 0755)
}
systemHome := "/etc"
if holenSystemEnv := system.Getenv("HOLEN_SYSTEM_CONFIG"); len(holenSystemEnv) > 0 {
systemHome = holenSystemEnv
}
var err error
systemConfigPath := filepath.Join(systemHome, "holenconfig")
if system.FileExists(systemConfigPath) {
systemConfigPath, err = filepath.EvalSymlinks(systemConfigPath)
if err != nil {
return nil, err
}
}
userConfigPath := filepath.Join(baseDir, "config")
if system.FileExists(userConfigPath) {
userConfigPath, err = filepath.EvalSymlinks(userConfigPath)
if err != nil {
return nil, err
}
}
configClient := RealConfigClient{
systemConfig: systemConfigPath,
userConfig: userConfigPath,
}
return &configClient, nil
}