forked from anchore/fangs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
52 lines (45 loc) · 1.41 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
package fangs
import (
"fmt"
"os"
"github.com/anchore/go-logger"
"github.com/anchore/go-logger/adapter/discard"
)
type Config struct {
Logger logger.Logger `yaml:"-" json:"-" mapstructure:"-"`
AppName string `yaml:"-" json:"-" mapstructure:"-"`
TagName string `yaml:"-" json:"-" mapstructure:"-"`
File string `yaml:"-" json:"-" mapstructure:"-"`
Finders []Finder `yaml:"-" json:"-" mapstructure:"-"`
}
var _ FlagAdder = (*Config)(nil)
// NewConfig creates a new Config object with defaults
func NewConfig(appName string) Config {
return Config{
Logger: discard.New(),
AppName: appName,
TagName: "mapstructure",
// search for configs in specific order
Finders: []Finder{
// 1. look for a directly configured file
FindDirect,
// 2. look for ./.<appname>.<ext>
FindInCwd,
// 3. look for ./.<appname>/config.<ext>
FindInAppNameSubdir,
// 4. look for ~/.<appname>.<ext>
FindInHomeDir,
// 5. look for <appname>/config.<ext> in xdg locations
FindInXDG,
},
}
}
// WithConfigEnvVar looks for the environment variable: <APP_NAME>_CONFIG as a way to specify a config file
// This will be overridden by a command-line flag
func (c Config) WithConfigEnvVar() Config {
c.File = os.Getenv(envVar(c.AppName, "CONFIG"))
return c
}
func (c *Config) AddFlags(flags FlagSet) {
flags.StringVarP(&c.File, "config", "c", fmt.Sprintf("%s configuration file", c.AppName))
}