Documentation
¶
Overview ¶
Package aconfig provides simple but still powerful config loader.
It can read configuration from different sources, like defaults, files, environment variables, console flag parameters.
Defaults are defined in structure tags (`default` tag). For files JSON, YAML, TOML and .Env are supported.
Environment variables and flag parameters can have an optional prefix to separate them from other entries.
Also, aconfig is dependency-free, file decoders are used as separate modules (submodules to be exact) and are added to your go.mod only when used.
Loader configuration (`Config` type) has different ways to configure loader, to skip some sources, define prefixes, fail on unknown params.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶ added in v0.9.0
type Config struct { SkipDefaults bool // SkipDefaults set to true will not load config from 'default' tag. SkipFiles bool // SkipFiles set to true will not load config from files. SkipEnv bool // SkipEnv set to true will not load config from environment variables. SkipFlags bool // SkipFlags set to true will not load config from flag parameters. EnvPrefix string // EnvPrefix for environment variables. FlagPrefix string // FlagPrefix for flag parameters. FlagDelimiter string // FlagDelimiter for flag parameters. If not set - default is ".". // AllFieldsRequired set to true will fail config loading if one of the fields was not set. // File, environment, flag must provide a value for the field. // If default is set and this option is enabled (or required tag is set) there will be an error. AllFieldRequired bool // AllowDuplicates set to true will not fail on duplicated names on fields (env, flag, etc...) AllowDuplicates bool // AllowUnknownFields set to true will not fail on unknown fields in files. AllowUnknownFields bool // AllowUnknownEnvs set to true will not fail on unknown environment variables (). // When false error is returned only when EnvPrefix isn't empty. AllowUnknownEnvs bool // AllowUnknownFlags set to true will not fail on unknown flag parameters (). // When false error is returned only when FlagPrefix isn't empty. AllowUnknownFlags bool // DontGenerateTags disables tag generation for JSON, YAML, TOML file formats. DontGenerateTags bool // FailOnFileNotFound will stop Loader on a first not found file from Files field in this structure. FailOnFileNotFound bool // FileSystem from which files will be loaded. Default is nil (OS file system). FileSystem fs.FS // MergeFiles set to true will collect all the entries from all the given files. // Easy wat to cobine base.yaml with prod.yaml MergeFiles bool // FileFlag the name of the flag that defines the path to the configuration file passed through the CLI. // (To make it easier to transfer the config file via flags.) FileFlag string // Files from which config should be loaded. Files []string // Envs hold the environment variable from which envs will be parsed. // By default is nil and then os.Environ() will be used. Envs []string // Args hold the command-line arguments from which flags will be parsed. // By default is nil and then os.Args will be used. // Unless loader.Flags() will be explicitly parsed by the user. Args []string // FileDecoders to enable other than JSON file formats and prevent additional dependencies. // Add required submodules to the go.mod and register them in this field. // Example: // FileDecoders: map[string]aconfig.FileDecoder{ // ".yaml": aconfigyaml.New(), // ".toml": aconfigtoml.New(), // ".env": aconfigdotenv.New(), // } FileDecoders map[string]FileDecoder // contains filtered or unexported fields }
Config to configure configuration loader.
type Field ¶ added in v0.4.0
type Field interface { // Name of the field. Name() string // Tag returns a given tag for a field. Tag(tag string) string // Parent of the current node. Parent() (Field, bool) }
Field of the user configuration structure. Done as an interface to export less things in lib.
type FileDecoder ¶ added in v0.8.0
type FileDecoder interface { Format() string DecodeFile(filename string) (map[string]interface{}, error) }
FileDecoder is used to read config from files. See aconfig submodules.
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader of user configuration.
func LoaderFor ¶ added in v0.2.1
LoaderFor creates a new Loader based on a given configuration structure. Supports only non-nil structures.
func (*Loader) Flags ¶ added in v0.1.2
Flags returngs flag.FlagSet to create your own flags. FlagSet name is Config.FlagPrefix and error handling is set to ContinueOnError.
func (*Loader) WalkFields ¶ added in v0.4.0
WalkFields iterates over configuration fields. Easy way to create documentation or user-friendly help.
Directories
¶
Path | Synopsis |
---|---|
aconfigdotenv
module
|
|
aconfighcl
module
|
|
aconfigtoml
module
|
|
aconfigyaml
module
|