-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
82 lines (73 loc) · 1.73 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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/olebedev/config"
)
func getConfig() (cfg *config.Config, err error) {
dirname, _ := os.Getwd()
files, err := ioutil.ReadDir(dirname)
if err != nil {
log.Error(err)
}
allConfigs := []*config.Config{{Root: DefaultConfig}}
for _, f := range files {
if !isCfgFile(f.Name()) {
continue
}
var c *config.Config
if strings.HasSuffix(f.Name(), ".yml") || strings.HasSuffix(f.Name(), ".yaml") {
c, err = config.ParseYamlFile(f.Name())
if err != nil {
return
}
allConfigs = append(allConfigs, c)
}
}
cfg = combineConfigs(allConfigs...)
return
}
func prettyPrintFlagMap(m map[string]interface{}, prefix ...string) {
for k, v := range m {
flagName := "-" + k
if len(prefix) > 0 {
flagName = "-" + strings.Join(prefix, "-") + flagName
}
switch v.(type) {
case string, int, bool:
fmt.Printf(" %s=%+v\n", flagName, v)
case map[string]interface{}:
prettyPrintFlagMap(v.(map[string]interface{}), append(prefix, k)...)
}
}
}
// combineConfigs converts n `*config.Config` objects to their underlying
// `map[string]interface{}` objects so we can recursively combine them with
// combineMaps.
func combineConfigs(cfgs ...*config.Config) *config.Config {
maps := []map[string]interface{}{}
for _, conf := range cfgs {
m := append(maps, conf.Root.(map[string]interface{}))
maps = m
}
root := combineMaps(maps...)
return &config.Config{
Root: root,
}
}
func isCfgFile(path string) bool {
file, err := os.Open(path)
if err != nil {
return false
}
defer file.Close()
scanner := bufio.NewScanner(file)
if scanner.Scan() && scanner.Text() == "#tlspxy" {
return true
}
return false
}