conf

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

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:

  1. BindDefaults with a tagged struct instance with the default values set.
  2. Use one or more "PreRun" functions, such as PreRunBindFlags, PreRunBindEnvMatching, and PreRunBindConfigFile, or those in the bind package.
  3. 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).
  4. 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ActiveConfig

func ActiveConfig() *config.Config

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

func BindDefaults(cfg any) error

BindDefaults binds a struct to the koanf instance. The field names should have `koanf:"name"` tags to bind the correct name.

func BindDefaultsWithRootDir

func BindDefaultsWithRootDir(cfg any, rootDir string) error

func PreRunBindConfigFile

func PreRunBindConfigFile(cmd *cobra.Command, args []string) error

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

func PreRunBindConfigFileStrict[T any](cmd *cobra.Command, args []string) error

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

func PreRunBindEarlyRootDirEnv(cmd *cobra.Command, args []string) error

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

func PreRunBindEarlyRootDirFlag(cmd *cobra.Command, args []string) error

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

func PreRunBindEnvAllSections(cmd *cobra.Command, args []string) error

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

func PreRunBindEnvMatching(cmd *cobra.Command, args []string) error

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

func PreRunBindFlags(cmd *cobra.Command, args []string) error

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

func PreRunPrintEffectiveConfig(cmd *cobra.Command, args []string) error

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.

func RootDir

func RootDir() string

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL