-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
193 lines (153 loc) · 3.35 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package config
import (
"bytes"
"encoding/json"
"golanger.com/log"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
"sync"
)
var (
mu sync.RWMutex
ignoreFirst byte = '.'
regexpNote *regexp.Regexp = regexp.MustCompile(`#.*`)
)
type Config struct {
dataType string
data []byte
target string
files []string
}
func format(configPath string) []byte {
data, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatal("<format> error: ", err)
}
return bytes.TrimSpace(regexpNote.ReplaceAll(data, []byte(``)))
}
func readFiles(files ...string) []byte {
lfs := len(files)
chContent := make(chan []byte, lfs)
for _, file := range files {
go func(chContent chan []byte, configPath string) {
chContent <- format(configPath)
}(chContent, file)
}
bytess := make([][]byte, 0, lfs)
for i := 1; i <= lfs; i++ {
content := <-chContent
if len(content) != 0 {
bytess = append(bytess, content)
}
}
buf := bytes.NewBufferString(`{`)
buf.Write(bytes.Join(bytess, []byte(`,`)))
buf.WriteString(`}`)
var contentBuf bytes.Buffer
err := json.Compact(&contentBuf, buf.Bytes())
if err != nil {
log.Debug("<readFiles> jsonData: ", buf.String())
log.Fatal("<readFiles> error: ", err)
}
return contentBuf.Bytes()
}
func loadFiles(files ...string) *Config {
filestmp := make([]string, 0, len(files))
for _, file := range files {
fileName := filepath.Base(file)
if fileName[0] == ignoreFirst {
continue
}
filestmp = append(filestmp, file)
}
return &Config{
data: readFiles(filestmp...),
files: filestmp,
}
}
func Data(data string) *Config {
data = `{` + data + `}`
var buf bytes.Buffer
err := json.Compact(&buf, []byte(data))
if err != nil {
log.Debug("<Data> jsonData: ", data)
log.Fatal("<Data> error: ", err)
}
return &Config{
dataType: "data",
data: buf.Bytes(),
}
}
func Files(files ...string) *Config {
conf := loadFiles(files...)
mu.Lock()
conf.dataType = "files"
conf.target = strings.Join(conf.files, ",")
mu.Unlock()
return conf
}
func Glob(pattern string) *Config {
files, err := filepath.Glob(pattern)
if err != nil {
log.Fatal("<Glob> error: ", err)
}
conf := loadFiles(files...)
mu.Lock()
conf.dataType = "glob"
conf.target = pattern
mu.Unlock()
return conf
}
func Dir(configDir string) *Config {
configDir = filepath.Clean(configDir)
fis, err := ioutil.ReadDir(configDir)
if err != nil {
log.Fatal("<Dir> error: ", err)
}
var files []string
for _, fi := range fis {
fileName := fi.Name()
if fi.IsDir() || fileName[0] == ignoreFirst {
continue
}
files = append(files, filepath.Join(configDir, fileName))
}
conf := loadFiles(files...)
mu.Lock()
conf.dataType = "directory"
conf.target = configDir
mu.Unlock()
return conf
}
func (c *Config) Load(i interface{}) *Config {
mu.RLock()
data := c.data
mu.RUnlock()
err := json.Unmarshal(data, i)
if err != nil {
log.Debug("<Config.Load> jsonData: ", c.String())
log.Fatal("<Config.Load> error: ", err)
}
return c
}
func (c *Config) indent() *bytes.Buffer {
mu.RLock()
data := c.data
mu.RUnlock()
buf := bytes.NewBuffer(nil)
json.Indent(buf, data, "", "\t")
return buf
}
func (c *Config) Bytes() []byte {
return c.indent().Bytes()
}
func (c *Config) String() string {
return c.indent().String()
}
func (c *Config) Target() string {
mu.RLock()
defer mu.RUnlock()
return c.target
}