-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_env.go
40 lines (33 loc) · 1.33 KB
/
opt_env.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
package configer
import "strings"
type envKeyReplacerOption string
func (r envKeyReplacerOption) apply(parser *configParser) {
replacer := strings.NewReplacer(".", string(r))
parser.viper.SetEnvKeyReplacer(replacer)
}
// WithEnvKeyReplacer allows to specify a string replacer in order to use a
// different separator in environment variables than the configuration option
// key. The separator used for configuration keys is "."
//
// e.g. if the replacer is set to "_", the flag bound to "env.variable" can be
// set via the environment variable "ENV_VARIABLE".
//
// By default, the parser uses the "_" separator for environment variables.
func WithEnvKeyReplacer(replacer string) envKeyReplacerOption {
return envKeyReplacerOption(replacer)
}
type envPrefixOptions string
func (p envPrefixOptions) apply(parser *configParser) {
parser.viper.SetEnvPrefix(string(p))
}
// WithEnvPrefix allows using a specific prefix for the environment variables
// associated with the project.
//
// e.g. if the prefix is set to "DEMO", for the option parser.option the parser
// associates the "DEMO_PARSER_OPTION" environment variable, assuming the
// replacer is set to "_"
//
// By default, the parser doesn't look for any prefix.
func WithEnvPrefix(prefix string) envPrefixOptions {
return envPrefixOptions(prefix)
}