Documentation
ΒΆ
Index ΒΆ
- Variables
- func CmdlineDecoder(r io.Reader) *cmdlineDecoder
- func EnvDecoder(r io.Reader) *envDecoder
- func Fill[T any](cfg *T, provider ConfigProvider, opts ...options.Option[cfgOpts]) error
- func FromCmdline() *cmdlineProvider
- func FromConsul(cfgPath string, opts ...ConsulOption) *consulProvider
- func FromEnv() *envProvider
- func FromEnvWithOptions(opts EnvOptions) *envProvider
- func FromFile(cfgPath string) *fileProvider
- func FromReader(r io.Reader) *readerProvider
- func IniDecoder(r io.Reader) *iniDecoder
- func Multi[T any]() *multiConfigurator[T]
- func New[T any](provider ConfigProvider, opts ...options.Option[cfgOpts]) (cfg T, err error)
- func WithDecoder[D Decoder](newDec func(r io.Reader) D) options.Option[cfgOpts]
- type ConfigProvider
- type ConsulOption
- type Decoder
- type EnvOptions
Constants ΒΆ
This section is empty.
Variables ΒΆ
var NoProvider = &noProvider{}
NoProvider is a no-op config provider. It always returns empty io.Reader.
Functions ΒΆ
func CmdlineDecoder ΒΆ
Returns cmdline decoder that parses cmd args to struct with tags. It uses under the hood jessevdk/go-flags library.
func EnvDecoder ΒΆ
Returns env decoder that parses envs to struct with tags. Provider should return json config or nothing. JSON config will be used as additional environment. From JSON config will be read to map[string]string, so variables must be string (other will be ignored). Use this decoder with provider NoProvider if you want variables only from environment. Recommended use with EnvProvider, because it provides variable $HOSTNAME. It uses under the hood caarlos0/env/v9 library.
func Fill ΒΆ
func Fill[T any](cfg *T, provider ConfigProvider, opts ...options.Option[cfgOpts]) error
Fill creates config and fills into cfg argument.
Provider provides data for decoding. By default, it uses decoder json.Decoder. Use WithDecoder to override the decoder. If the reader implements the io.Closer interface, then it will be closed.
For example we want to create config with default values and then fill config.
type Config struct {
Foo string `json:"foo"`
}
func main() {
cfg := Config{
Foo: "default"
}
err := config.Fill(&cfg, config.FromConsul"/bar/foo"))
//...
}
func FromCmdline ΒΆ
func FromCmdline() *cmdlineProvider
Returns config provider that provides config from cmdline arguments.
func FromConsul ΒΆ
func FromConsul(cfgPath string, opts ...ConsulOption) *consulProvider
Returns config provider that provides config from consul kv.
If client wasn't passed with options, it's created with default config.
By default, config will use CONSUL_HTTP_ADDR env as HTTP address. If it's empty, localhost will be chosen.
func FromEnv ΒΆ
func FromEnv() *envProvider
FromEnv returns provider that provides env variables in json form.
Example:
// Env
FOO=bar
BUZ=foo
// converted to
{"FOO": "bar", "BUZ": "foo"}
func FromEnvWithOptions ΒΆ
func FromEnvWithOptions(opts EnvOptions) *envProvider
FromEnvWithOptions returns provider that provides env variables in json form.
Example:
// Env
FOO=bar
BUZ=foo
// converted to
{"FOO": "bar", "BUZ": "foo"}
func FromFile ΒΆ
func FromFile(cfgPath string) *fileProvider
FromFile creates a new config provider from a config file.
func FromReader ΒΆ
FromReader returns a ConfigProvider that reads from r.
func IniDecoder ΒΆ added in v1.1.0
func Multi ΒΆ
func Multi[T any]() *multiConfigurator[T]
Multi creates multi configurator that aggregates different methods of creating config. Use method .Add to add configurator, then call .OneOf or .AllOf method to build config.
NOTE: result depends on providers order. If parameter is provided by multiple of them, the last wins.
Example
cfg, err := config.Multi[MyConfig](). Add(config.FromReader(file)). Add(config.FromEnv(), config.WithDecoder(config.EnvDecoder)). Add(config.FromCmdline(), config.WithDecoder(config.CmdlineDecoder)). AllOf()
func New ΒΆ
func New[T any](provider ConfigProvider, opts ...options.Option[cfgOpts]) (cfg T, err error)
Creates config T where provider provides data for decoding. By default, it uses json.Decoder. Use WithDecoder to override the decoder. If the reader implements the io.Closer interface, then it will be closed.
Types ΒΆ
type ConfigProvider ΒΆ
ConfigProvider is an interface that provides configuration data.
type ConsulOption ΒΆ
type ConsulOption func(*consulOpts) error
func ConsulWithClient ΒΆ
func ConsulWithClient(client *consul.Client) ConsulOption
Overrides consul client.
func ConsulWithQueryOptions ΒΆ
func ConsulWithQueryOptions(qry *consul.QueryOptions) ConsulOption
Defines the query options that will be used when lookuping for the config.
type Decoder ΒΆ
Decoder is an interface that decodes configuration data into an object. The object must be pointer.