Documentation
¶
Overview ¶
Package conf is used to merge multiple node config sources. It encapsulates the koanf package, which is functionally similar to viper.
A cobra command should:
- BindDefaults with a tagged struct instance with the default values set.
- Use one or more "PreRun" functions, such as PreRunBindFlags, PreRunBindEnvMatching, and PreRunBindConfigFile, or those in the bind package.
- Use the bind.ChainPreRuns helper to combine the preruns in the desired order, which dictates the merge priority for the config sources (e.g. env > flags > toml).
- Use ActiveConfig to get the merged config. Any subcommand can access the config this way.
See also the app/shared/bind package for shared helper functions for working with flags and koanf instances.
Index ¶
- func ActiveConfig() *config.Config
- func BindDefaults(cfg any) error
- func BindDefaultsWithRootDir(cfg any, rootDir string) error
- func PreRunBindConfigFile(cmd *cobra.Command, args []string) error
- func PreRunBindConfigFileStrict[T any](cmd *cobra.Command, args []string) error
- func PreRunBindEarlyRootDirEnv(cmd *cobra.Command, args []string) error
- func PreRunBindEarlyRootDirFlag(cmd *cobra.Command, args []string) error
- func PreRunBindEnvAllSections(cmd *cobra.Command, args []string) error
- func PreRunBindEnvMatching(cmd *cobra.Command, args []string) error
- func PreRunBindFlags(cmd *cobra.Command, args []string) error
- func PreRunPrintEffectiveConfig(cmd *cobra.Command, args []string) error
- func RootDir() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ActiveConfig ¶
ActiveConfig retrieves the current merged config. This is influenced by the other functions in this package, including: BindDefaults, SetFlagsFromStruct, PreRunBindFlags, PreRunBindEnvMatching, PreRunBindEnvAllSections, and PreRunBindConfigFile. The merge result is determined by the order in which they source are bound (see PreRunBindConfigFile).
func BindDefaults ¶
BindDefaults binds a struct to the koanf instance. The field names should have `koanf:"name"` tags to bind the correct name.
func BindDefaultsWithRootDir ¶
func PreRunBindConfigFile ¶
PreRunBindConfigFile loads and merges settings from the config file. This used shared.RootDir to get the root directory. As such, the command should be use shared.BindRootDirVar.
func PreRunBindConfigFileStrict ¶
PreRunBindConfigFileStrict is like PreRunBindConfigFile, but it strictly requires that the parsed TOML does not contain any fields that are not recognized present in the config struct, which is specified by the type parameter T.
func PreRunBindEarlyRootDirEnv ¶
PreRunBindEarlyRootDirEnv updates the active config's root directory from KWILD_ROOT. This allows downstream sources, such as config file loading, to use the root directory, while having the other environment variables override config file values.
func PreRunBindEarlyRootDirFlag ¶
PreRunBindEarlyRootDirFlag is like PreRunBindEarlyRootDirEnv, but for the root flag. These two "early" preruns are separate from each other as well as from the other preruns such as PreRunBindConfigFile.
func PreRunBindEnvAllSections ¶
PreRunBindEnvAllSections treats all underscores as section delimiters. With this approach, the config merge process can work with the following conventions:
toml: section.sub.two_words (replacing "_" with "") flag: --section.sub.two-words (replacing "-" with "") env: KWIL_SECTION_SUB_TWOWORDS (no underscores allowed in key name!)
To merge all, k.Load from each source should merge by standardizing the key names into "twowords", AND the `koanf` tag should match.
func PreRunBindEnvMatching ¶
PreRunBindEnvMatching is to accomplish all of the following:
KWIL_SECTION_SUBSECTION_SOME_KEY => section.subsection.some-key KWIL_SECTION_SUBSECTION_KEYNAME => section.subsection.keyname KWIL_SECTION_SOME_KEY => section.some-key KWIL_SECTION_KEYNAME => section.keyname
For this to work correctly, a previously loaded default config struct (PreRunBindConfigFile) or pflags (PreRunBindFlags). If neither sources preceded env, they are loaded assuming every "_" is a section delimiter, which may be incorrect for multi-word keys like KWIL_SECTION_SOME_KEY.
func PreRunBindFlags ¶
PreRunBindFlags loads flags and standardizes the keys to "_" instead of "-". This is done to unify the config file and flags. Be sure to define the flags in the command's flag set. See bind.SetFlagsFromStruct to automate defining the flags from a default config struct.
func PreRunPrintEffectiveConfig ¶
PreRunPrintEffectiveConfig prints the effective config map if CLI debugging is enabled (the `--debug` flag is set), otherwise this does nothing. It may be specified multiple times in a PreRun chain.
Types ¶
This section is empty.