Documentation ¶
Index ¶
- func NewConfig(configStruct interface{}, appOptions []ConfigOption, ...) error
- func WithConfigFile(configFile string) configFileOption
- func WithConfigName(configName string) configNameOption
- func WithConfigPath(path string) configPathOption
- func WithConfigType(configType string) configTypeOption
- func WithEnvKeyReplacer(replacer string) envKeyReplacerOption
- func WithEnvPrefix(prefix string) envPrefixOptions
- func WithReadFlag() readFlagOption
- func WithSupressLogs() suppressLogsOption
- func WithWriteFlag() writeFlagOption
- type ConfigOption
- type ParserOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewConfig ¶
func NewConfig(configStruct interface{}, appOptions []ConfigOption, parserOptions ...ParserOption) error
NewConfig generates a new configuration setting for the project, based on the provided config options. It unmarshals the options to the provided struct, which can then be used in the project to read those options.
func WithConfigFile ¶
func WithConfigFile(configFile string) configFileOption
WithConfigFile allows providing a specific configuration file for the parser. This will overwrite all the options that have been specified through WithConfigPath, including the default ones.
This option is not set by default.
func WithConfigName ¶
func WithConfigName(configName string) configNameOption
WithConfigName allows specifying the name of the configuration file the parser looks for. Currently, the parser supports searching for a single file name.
By default, the parser searches for a configuration file named "config".
func WithConfigPath ¶
func WithConfigPath(path string) configPathOption
WithConfigPath allows specifying paths where the parser should search for a configuration options file. The format of this configuration file may be defined using the functions WithConfigType and WithConfigName.
By default, the parser searches for configuration files in the project directory.
func WithConfigType ¶
func WithConfigType(configType string) configTypeOption
WithConfigType allows specifying the type of the configuration file the parser looks for. Currently, the parser supports searching for a single file type.
By default, the parser searches for yaml configuration files.
func WithEnvKeyReplacer ¶
func WithEnvKeyReplacer(replacer string) envKeyReplacerOption
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 WithEnvPrefix ¶
func WithEnvPrefix(prefix string) envPrefixOptions
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 WithReadFlag ¶
func WithReadFlag() readFlagOption
WithReadFlag defines a flag that can be used to explicitly set the location from where the configuration file is read. It ignores the options that were set through WithConfigPath.
If the specified location is a directory, the parser will search for a config file with the name and type specified via WithConfigName and WithConfigType, or the default values if those were not set. Otherwise, if the location is a file, it will try to read the config from that file. If no config file is found, the parser will throw an error.
By default, this flag will not be defined.
func WithSupressLogs ¶ added in v0.3.0
func WithSupressLogs() suppressLogsOption
WithSupressLogs suppresses the logs generated by creating a new parser.
By default, logs are active.
func WithWriteFlag ¶
func WithWriteFlag() writeFlagOption
WithWriteFlag defines a flag that can be used to write the project configuration to a file.
If the specified location is a directory, the parser will search for a config file with the name and type specified via WithConfigName and WithConfigType, or the default values if those were not set. Otherwise, if the location is a file, it will try to read the config from that file.
If there already is a configuration file at the specified location, this option will overwrite that file.
By default, this flag will not be defined.
Types ¶
type ConfigOption ¶
type ConfigOption struct { // The name of the flag associated with the configuration option. May be // empty if the option is associated with no flag. FlagName string // The shorthand of the flag. Must be empty if a flagname is not specified. Shorthand string // The value of the configuration option. Value any // The description of the configuration option. May be displayed in the // command line using the --help flag. Usage string // The key associated with the configuration option. If desired to associate // the key with the field of a struct, should be named the same as the // struct field (case insensitive). If the field is also a struct, the "." // separator should be used for the config key. // // E.g. if binding to the struct below, // // ========================= // | type Config struct { | // | Server Server | // | } | // | | // | type Server struct { | // | Address int | // | } | // ========================= // // the value of the config option with key "server.address" would get // unmarshaled to the server's address in the struct. ConfigKey string }
ConfigOption defines the values for each coniguration option of a microservice.
type ParserOption ¶
type ParserOption interface {
// contains filtered or unexported methods
}
ParserOption is an interface which allows implementing the functional options pattern for the config parser. By default, the package comes with some default options, and offers the possibility to overwrite them in every project using this package by using the WithXXX public functions exported below.