generated from sv-tools/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
44 lines (37 loc) · 940 Bytes
/
reader.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
package confflags
import (
"context"
"github.com/spf13/pflag"
"github.com/sv-tools/conf"
)
type flagReader struct {
mapFlagKey map[string]string
flagSet *pflag.FlagSet
prefix string
}
func (r *flagReader) Prefix() string {
return r.prefix
}
func (r *flagReader) Read(ctx context.Context) (interface{}, error) {
res := map[string]string{}
for name, key := range r.mapFlagKey {
if fl := r.flagSet.Lookup(name); fl != nil {
res[key] = fl.Value.String()
}
}
return res, ctx.Err()
}
// New creates the Env reader
//
// `mapFlagKey` is a map of the names of the flag and the configuration keys
// `prefix` is a default prefix that will be added to all configuration keys
func New(mapFlagKey map[string]string, prefix string, flagSet *pflag.FlagSet) conf.Reader {
if flagSet == nil {
flagSet = pflag.CommandLine
}
return &flagReader{
mapFlagKey: mapFlagKey,
prefix: prefix,
flagSet: flagSet,
}
}