Documentation ¶
Overview ¶
Package conf provides a set of utilities for mapping configuration settings (from env vars, flags and secret managers) to struct fields.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetFlag ¶
GetFlag is a utility to extract a flag from a slice of CLI args. It returns the value of the flag and a boolean indicating whether the flag was found. For example, args could be os.Args[1:]. flag should include the prefix, eg: "--verbose" or "-v" GetFlag supports the following formats:
flag=value flag="value" flag='value' flag value flag "value" flag 'value'
Example ¶
args := []string{"nonsense", "--xyz=abc", "nonsense", "-v"} xyz, _ := GetFlag("--xyz", args) _, verbose := GetFlag("-v", args) fmt.Printf("xyz = %q, verbose = %v", xyz, verbose)
Output: xyz = "abc", verbose = true
func LoadEnv ¶
LoadEnv recursively scans struct fields for the env tag then sets the values from the corresponding env var. Eg:
type Config struct { Host string `env:"HOST"` }
func LoadFlags ¶
LoadFlags recursively scans struct fields for the `flag` tag then sets the values from CLI flags. Eg:
type Config struct { Host string `flag:"--host"` Verbose bool `flag:"-v"` }
func LoadSecrets ¶
func LoadSecrets(ptr any, source SecretSource) error
LoadSecrets recursively scans struct fields for the secret tag then sets the values from the secret SecretSource. Eg:
type Config struct { Host string `secret:"host"` }
func Print ¶
func Print(ptr any)
Print wraps PrintToString and prints the result to stdout. Example output:
Host = "localhost" Verbose = false DB .Name = "app" .User = "user" .Pass ***
func PrintToString ¶
PrintToString returns a string representation of the config struct. Secrets are masked. Example output:
Host = "localhost" Verbose = false DB .Name = "app" .User = "user" .Pass ***
Types ¶
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field represents a struct field
func FlattenStructFields ¶
FlattenStructFields returns a flat slice of Field from recursively traversing the struct fields of v.
- unexported fields are omitted
- fields marked with an env, flag or secret tag are included, but their children are not
func (*Field) EnvVar ¶
EnvVar returns the `env` tag value and a bool indicating if the field has the `env` tag.
func (*Field) ExportValue ¶
ExportValue returns the value of the field as a string. If the field is not a string it will be marshalled to JSON.
func (*Field) FlagName ¶
FlagName returns the `flag` tag value and a bool indicating if the field has the `flag` tag.
type SecretSource ¶
type SecretSource interface { // Load a secret from the source. Returns the secret value, a boolean indicating if the secret was found and an error. // NOTE: Load should not return an error if the secret was not found, but should instead return "", false, nil. Load(key string) (string, bool, error) }
SecretSource interface allows any secret manager to be used, by wrapping it in a type that implements this interface.