-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
108 lines (95 loc) · 2.06 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
package delayed_job
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
)
var default_actuals = map[string]string{}
func loadActualFlags(flagSet *flag.FlagSet) map[string]string {
actual := map[string]string{}
fn := func(f *flag.Flag) {
actual[f.Name] = f.Name
}
if nil == flagSet {
flag.Visit(fn)
} else {
flagSet.Visit(fn)
}
return actual
}
func loadConfig(nm string, flagSet *flag.FlagSet, isOverride bool) error {
f, e := os.Open(nm)
if nil != e {
return fmt.Errorf("load config '%s' failed, %v", nm, e)
}
var res map[string]interface{}
e = json.NewDecoder(f).Decode(&res)
if nil != e {
return fmt.Errorf("load config '%s' failed, %v", nm, e)
}
actual := map[string]string{}
fn := func(f *flag.Flag) {
actual[f.Name] = f.Name
}
if nil == flagSet {
flag.Visit(fn)
} else {
flagSet.Visit(fn)
}
e = assignFlagSet("", res, flagSet, actual, isOverride)
if nil != e {
return fmt.Errorf("load config '%s' failed, %v", nm, e)
}
return nil
}
func assignFlagSet(prefix string, res map[string]interface{}, flagSet *flag.FlagSet, actual map[string]string, isOverride bool) error {
for k, v := range res {
switch value := v.(type) {
case map[string]interface{}:
e := assignFlagSet(combineName(prefix, k), value, flagSet, actual, isOverride)
if nil != e {
return e
}
continue
case []interface{}:
case string:
case float64:
case bool:
case nil:
continue
default:
return fmt.Errorf("unsupported type for %s - %T", combineName(prefix, k), v)
}
nm := combineName(prefix, k)
if !isOverride {
if _, ok := actual[nm]; ok {
log.Printf("load flag '%s' from config is skipped.\n", nm)
continue
}
}
var g *flag.Flag
if nil == flagSet {
g = flag.Lookup(nm)
} else {
g = flagSet.Lookup(nm)
}
if nil == g {
log.Printf("flag '%s' is not defined.\n", nm)
continue
}
err := g.Value.Set(fmt.Sprint(v))
if nil != err {
return err
}
log.Println("set", nm, "=", v)
}
return nil
}
func combineName(prefix, nm string) string {
if "" == prefix {
return nm
}
return prefix + "." + nm
}