Apply explicit CLI configuration overrides
This commit is contained in:
@@ -115,3 +115,22 @@ func ExampleSecretEnv() {
|
||||
fmt.Println(result.Config.APIKey.EnvName(), result.Config.APIKey.IsResolved(), err)
|
||||
// Output: CONFIGKIT_EXAMPLE_API_KEY true <nil>
|
||||
}
|
||||
|
||||
func ExampleOverride() {
|
||||
type Config struct {
|
||||
Workers int `yaml:"workers"`
|
||||
}
|
||||
workers, workersWasSet := 0, true // Supplied by the application's CLI parser.
|
||||
var overrides []configkit.Override
|
||||
if workersWasSet {
|
||||
overrides = append(overrides, configkit.Override{
|
||||
Path: configkit.Keys("workers"), Value: workers, Flag: "--workers",
|
||||
})
|
||||
}
|
||||
result, err := configkit.Load(configkit.NoSource(), configkit.Options[Config]{
|
||||
Definition: configkit.Definition[Config]{Defaults: func() Config { return Config{Workers: 4} }},
|
||||
Overrides: overrides,
|
||||
})
|
||||
fmt.Println(result.Config.Workers, result.Sources()[0].Label, err)
|
||||
// Output: 0 --workers <nil>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user