forked from insolar/insconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_path_getters.go
48 lines (40 loc) · 1.18 KB
/
config_path_getters.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
package insconfig
import (
goflag "flag"
flag "github.com/spf13/pflag"
)
// DefaultPathGetter adds "--config" flag and read path from it
type DefaultPathGetter struct {
GoFlags *goflag.FlagSet
}
func (g *DefaultPathGetter) GetConfigPath() string {
configPath := flag.String("config", "", "path to config")
flag.Parse()
return *configPath
}
// FlagPathGetter made for go flags compatibility
// Adds "--config" flag and read path from it, custom go flags should be created before and set to GoFlags
type FlagPathGetter struct {
GoFlags *goflag.FlagSet
}
func (g *FlagPathGetter) GetConfigPath() string {
if g.GoFlags != nil {
flag.CommandLine.AddGoFlagSet(g.GoFlags)
}
configPath := flag.String("config", "", "path to config")
flag.Parse()
return *configPath
}
// PFlagPathGetter made for spf13/pflags compatibility.
// Adds "--config" flag and read path from it, custom pflags should be created before and set to PFlags
type PFlagPathGetter struct {
PFlags *flag.FlagSet
}
func (g *PFlagPathGetter) GetConfigPath() string {
if g.PFlags != nil {
flag.CommandLine.AddFlagSet(g.PFlags)
}
configPath := flag.String("config", "", "path to config")
flag.Parse()
return *configPath
}