Add typed environment secret references
This commit is contained in:
@@ -3,6 +3,7 @@ package configkit_test
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
@@ -94,3 +95,23 @@ func ExampleResolution() {
|
||||
fmt.Println(strings.Join(order, " then "), result.Config.Derived, err)
|
||||
// Output: resolve then validate 6 <nil>
|
||||
}
|
||||
|
||||
func ExampleSecretEnv() {
|
||||
const name = "CONFIGKIT_EXAMPLE_API_KEY"
|
||||
previous, present := os.LookupEnv(name)
|
||||
_ = os.Setenv(name, "disposable-example-value")
|
||||
defer func() {
|
||||
if present {
|
||||
_ = os.Setenv(name, previous)
|
||||
} else {
|
||||
_ = os.Unsetenv(name)
|
||||
}
|
||||
}()
|
||||
|
||||
type Config struct {
|
||||
APIKey configkit.Secret `yaml:"api_key"`
|
||||
}
|
||||
result, err := configkit.Load[Config](configkit.Bytes("secret.yml", []byte("api_key: "+name+"\n")))
|
||||
fmt.Println(result.Config.APIKey.EnvName(), result.Config.APIKey.IsResolved(), err)
|
||||
// Output: CONFIGKIT_EXAMPLE_API_KEY true <nil>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user