Documentation ¶
Overview ¶
Package conf provides a set of utilities for mapping configuration settings (from env vars, flags and secret managers) to struct fields.
Index ¶
- func GetFlag(flag string, args []string) (val string, found bool)
- func Load[T any](cfg LoadCfg) (T, error)
- func LoadEnv(ptr any) error
- func LoadFlags(ptr any) error
- func LoadSecrets(ptr any, source SecretsLoader) error
- func MustLoad[T any](cfg LoadCfg) T
- func Print(ptr any)
- func PrintToString(ptr any) string
- type Field
- type LoadCfg
- type SecretsLoader
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 Load ¶ added in v1.1.0
Load config from multiple sources. T should be a struct with tagged fields:
- secrets: `secret:mySecretValue`
- env vars: `env:MY_ENV_VAR`
- CLI flags: `flag:--flag`
Sources are loaded in the following order:
- First load secrets from SecretsLoader (if not nil)
- Then environment variables - which will override secrets
- Finally command line flags - which override both secrets and env vars
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 SecretsLoader) error
LoadSecrets recursively scans struct fields for the secret tag then sets the values from the secret SecretsLoader. Eg:
type Config struct { Host string `secret:"host"` }
func MustLoad ¶ added in v1.1.0
MustLoad is a wrapper for Load which will panic if Load returns an error.
Example ¶
package main import ( "os" "strings" "github.com/fritzkeyzer/conf" ) func main() { type Cfg struct { Debug bool `flag:"--debug"` // enable debug logs Host string `flag:"--host" env:"HOST"` // host URL DB struct { User string `env:"DB_USER" flag:"--db-user"` Pass string `env:"DB_PASS" flag:"--db-pass" secret:"db-pass"` } } // fake env vars and cli flags _ = os.Setenv("HOST", "localhost:1111") _ = os.Setenv("DB_USER", "not_the_root_user") fakeCliFlags := " --debug --host=localhost:8888" // notice how the flag here will override the env var os.Args = strings.Split(fakeCliFlags, " ") // one-liner loads the config cfg := conf.MustLoad[Cfg](conf.LoadCfg{Env: true, Flags: true}) conf.Print(cfg) }
Output: ----------------------------------- Debug = true Host = "localhost:8888" DB .User = "not_the_root_user" .Pass *** (len=0) -----------------------------------
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 ¶ added in v1.0.3
type Field struct {
// contains filtered or unexported fields
}
Field represents a struct field
func FlattenStructFields ¶ added in v1.0.3
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 ¶ added in v1.0.3
EnvVar returns the `env` tag value and a bool indicating if the field has the `env` tag.
func (*Field) ExportValue ¶ added in v1.0.3
ExportValue returns the value of the field as a string.
- []byte fields are base64 encoded
- string fields are not pre-processed
- all other types marshalled to JSON
type LoadCfg ¶ added in v1.1.0
type LoadCfg struct { Env bool Flags bool SecretsLoader SecretsLoader }
type SecretsLoader ¶ added in v1.1.0
type SecretsLoader 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) ([]byte, bool, error) }
SecretsLoader interface allows any secret manager to be used, by wrapping it in a type that implements this interface.