2021-05-21 11:50:40 +00:00
|
|
|
package config
|
|
|
|
|
2023-04-25 07:04:26 +00:00
|
|
|
import "github.com/spf13/viper"
|
|
|
|
|
|
|
|
type opts struct {
|
|
|
|
path string
|
|
|
|
configDir string
|
|
|
|
envPrefix string
|
|
|
|
v *viper.Viper
|
2021-05-21 11:50:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 07:04:26 +00:00
|
|
|
func defaultOpts() *opts {
|
|
|
|
return new(opts)
|
2021-05-21 11:50:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Option allows to set an optional parameter of the Config.
|
2023-04-25 07:04:26 +00:00
|
|
|
type Option func(*opts)
|
2021-05-21 11:50:40 +00:00
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// WithConfigFile returns an option to set the system path
|
2021-05-21 11:50:40 +00:00
|
|
|
// to the configuration file.
|
|
|
|
func WithConfigFile(path string) Option {
|
2023-04-25 07:04:26 +00:00
|
|
|
return func(o *opts) {
|
|
|
|
o.path = path
|
2021-05-21 11:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-06 13:53:16 +00:00
|
|
|
|
|
|
|
// WithConfigDir returns an option to set the system path
|
|
|
|
// to the directory with configuration files.
|
|
|
|
func WithConfigDir(path string) Option {
|
2023-04-25 07:04:26 +00:00
|
|
|
return func(o *opts) {
|
|
|
|
o.configDir = path
|
2023-02-06 13:53:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-20 09:17:03 +00:00
|
|
|
|
|
|
|
// WithEnvPrefix returns an option to defines
|
|
|
|
// a prefix that ENVIRONMENT variables will use.
|
|
|
|
func WithEnvPrefix(envPrefix string) Option {
|
2023-04-25 07:04:26 +00:00
|
|
|
return func(o *opts) {
|
|
|
|
o.envPrefix = envPrefix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithViper returns an option to defines
|
|
|
|
// a predefined viper.Viper.
|
|
|
|
func WithViper(v *viper.Viper) Option {
|
|
|
|
return func(o *opts) {
|
|
|
|
o.v = v
|
2023-04-20 09:17:03 +00:00
|
|
|
}
|
|
|
|
}
|