github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/cfg/dynamic.go (about)

     1  package cfg
     2  
     3  import (
     4  	"flag"
     5  )
     6  
     7  // DynamicCloneable must be implemented by config structs that can be dynamically unmarshalled
     8  type DynamicCloneable interface {
     9  	Cloneable
    10  	ApplyDynamicConfig() Source
    11  }
    12  
    13  // DynamicUnmarshal handles populating a config based on the following precedence:
    14  // 1. Defaults provided by the `RegisterFlags` interface
    15  // 2. Sections populated by dynamic logic. Configs passed to this function must implement ApplyDynamicConfig()
    16  // 3. Any config options specified directly in the config file
    17  // 4. Any config options specified on the command line.
    18  func DynamicUnmarshal(dst DynamicCloneable, args []string, fs *flag.FlagSet) error {
    19  	return Unmarshal(dst,
    20  		// First populate the config with defaults including flags from the command line
    21  		Defaults(fs),
    22  		// Next populate the config from the config file, we do this to populate the `common`
    23  		// section of the config file by taking advantage of the code in ConfigFileLoader which will load
    24  		// and process the config file.
    25  		ConfigFileLoader(args, "config.file"),
    26  		// Apply any dynamic logic to set other defaults in the config. This function is called after parsing the
    27  		// config files so that values from a common, or shared, section can be used in
    28  		// the dynamic evaluation
    29  		dst.ApplyDynamicConfig(),
    30  		// Load configs from the config file a second time, this will supersede anything set by the common
    31  		// config with values specified in the config file.
    32  		ConfigFileLoader(args, "config.file"),
    33  		// Load the flags again, this will supersede anything set from config file with flags from the command line.
    34  		Flags(args, fs),
    35  	)
    36  }