-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fill_args.go
118 lines (91 loc) · 2.76 KB
/
fill_args.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
package goconfig
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"reflect"
"strings"
"time"
)
var values = map[string]interface{}{}
type postFillArgs struct {
item
Raw *string
}
func FillArgs(c interface{}, args []string) error {
var f = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
f.Usage = func() {}
f.SetOutput(os.Stdout)
// Default config flag
f.String("config", "", "Configuration JSON file")
post := []postFillArgs{}
traverse(c, func(i item) {
name_path := strings.ToLower(strings.Join(i.Path, "."))
if reflect.TypeOf(time.Duration(0)) == i.Value.Type() {
value := ""
f.StringVar(&value, name_path, i.Value.Interface().(time.Duration).String(), i.Usage)
post = append(post, postFillArgs{
Raw: &value,
item: i,
})
} else if reflect.Bool == i.Kind {
f.BoolVar(i.Ptr.(*bool), name_path, i.Value.Interface().(bool), i.Usage)
} else if reflect.Float64 == i.Kind {
f.Float64Var(i.Ptr.(*float64), name_path, i.Value.Interface().(float64), i.Usage)
} else if reflect.Int64 == i.Kind {
f.Int64Var(i.Ptr.(*int64), name_path, i.Value.Interface().(int64), i.Usage)
} else if reflect.Int == i.Kind {
f.IntVar(i.Ptr.(*int), name_path, i.Value.Interface().(int), i.Usage)
} else if reflect.String == i.Kind {
f.StringVar(i.Ptr.(*string), name_path, i.Value.Interface().(string), i.Usage)
} else if reflect.Uint64 == i.Kind {
f.Uint64Var(i.Ptr.(*uint64), name_path, i.Value.Interface().(uint64), i.Usage)
} else if reflect.Uint == i.Kind {
f.UintVar(i.Ptr.(*uint), name_path, i.Value.Interface().(uint), i.Usage)
} else if reflect.Slice == i.Kind {
b, _ := json.Marshal(i.Value.Interface())
value := ""
f.StringVar(&value, name_path, string(b), i.Usage)
post = append(post, postFillArgs{
Raw: &value,
item: i,
})
} else {
panic("Kind `" + i.Kind.String() +
"` is not supported by goconfig (field `" + i.FieldName + "`)")
}
})
if err := f.Parse(args); err != nil && err == flag.ErrHelp {
m := bytes.NewBufferString("Usage of goconfig:\n\n")
f.SetOutput(m)
f.PrintDefaults()
return errors.New(m.String())
}
// Postprocess flags: unsupported flags needs to be declared as string
// and parsed later. Here is the place.
for _, p := range post {
// Special case for durations
if reflect.TypeOf(time.Duration(0)) == p.Value.Type() {
d, err := unmarshalDurationString(*p.Raw)
if err != nil {
return fmt.Errorf(
"'%s' should be nanoseconds or a time.Duration string: %s",
p.FieldName, err.Error(),
)
}
p.Value.SetInt(int64(d))
continue
}
err := json.Unmarshal([]byte(*p.Raw), p.Ptr)
if err != nil {
return errors.New(fmt.Sprintf(
"'%s' should be a JSON array: %s",
p.FieldName, err.Error(),
))
}
}
return nil
}