config.go101 lines, 2.8 KB
| 1 | // Package config reads service configuration from environment variables. |
| 2 | package config |
| 3 | |
| 4 | import ( |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "slices" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // Env reads typed values from environment variables. It doesn't stop at the |
| 14 | // first problem: it collects them all, so a misconfigured service reports |
| 15 | // everything that's wrong in one go. |
| 16 | type Env struct { |
| 17 | lookup func(key string) (string, bool) |
| 18 | problems []string |
| 19 | } |
| 20 | |
| 21 | // New returns an Env that reads variables with lookup, which is os.LookupEnv |
| 22 | // in a real service. Tests pass a function backed by a map. |
| 23 | func New(lookup func(key string) (string, bool)) *Env { |
| 24 | return &Env{lookup: lookup} |
| 25 | } |
| 26 | |
| 27 | // get returns a variable's value without surrounding spaces, and whether it |
| 28 | // has one. A variable that is set but empty counts as unset. |
| 29 | func (e *Env) get(key string) (string, bool) { |
| 30 | v, _ := e.lookup(key) |
| 31 | v = strings.TrimSpace(v) |
| 32 | return v, v != "" |
| 33 | } |
| 34 | |
| 35 | // Invalid records a problem with a variable. |
| 36 | func (e *Env) Invalid(key, problem string) { |
| 37 | e.problems = append(e.problems, key+": "+problem) |
| 38 | } |
| 39 | |
| 40 | // Err returns nil if every variable was valid. Otherwise it returns a single |
| 41 | // error that lists every problem, one per line. |
| 42 | func (e *Env) Err() error { |
| 43 | if len(e.problems) == 0 { |
| 44 | return nil |
| 45 | } |
| 46 | return errors.New("invalid configuration:\n " + strings.Join(e.problems, "\n ")) |
| 47 | } |
| 48 | |
| 49 | // String returns the variable's value, or def if it is unset. |
| 50 | func (e *Env) String(key, def string) string { |
| 51 | if v, ok := e.get(key); ok { |
| 52 | return v |
| 53 | } |
| 54 | return def |
| 55 | } |
| 56 | |
| 57 | // Required returns the variable's value, and records a problem if it is unset. |
| 58 | func (e *Env) Required(key string) string { |
| 59 | v, ok := e.get(key) |
| 60 | if !ok { |
| 61 | e.Invalid(key, "required, but not set") |
| 62 | } |
| 63 | return v |
| 64 | } |
| 65 | |
| 66 | // OneOf returns the variable's value, or def if it is unset, and records a |
| 67 | // problem if the value isn't one of allowed. |
| 68 | func (e *Env) OneOf(key, def string, allowed ...string) string { |
| 69 | v := e.String(key, def) |
| 70 | if !slices.Contains(allowed, v) { |
| 71 | e.Invalid(key, fmt.Sprintf("%q is not one of %s", v, strings.Join(allowed, ", "))) |
| 72 | } |
| 73 | return v |
| 74 | } |
| 75 | |
| 76 | // Parse returns the variable converted by parse, or def if it is unset. A |
| 77 | // value that doesn't parse is recorded as a problem. It's a function, not a |
| 78 | // method, because Go methods can't have type parameters. |
| 79 | func Parse[T any](e *Env, key string, def T, parse func(string) (T, error)) T { |
| 80 | v, ok := e.get(key) |
| 81 | if !ok { |
| 82 | return def |
| 83 | } |
| 84 | x, err := parse(v) |
| 85 | if err != nil { |
| 86 | e.Invalid(key, fmt.Sprintf("invalid value %q", v)) |
| 87 | return def |
| 88 | } |
| 89 | return x |
| 90 | } |
| 91 | |
| 92 | // Int returns the variable as a whole number, or def if it is unset. |
| 93 | func (e *Env) Int(key string, def int) int { |
| 94 | return Parse(e, key, def, strconv.Atoi) |
| 95 | } |
| 96 | |
| 97 | // Duration returns the variable as a duration such as 5s or 1m30s, or def if |
| 98 | // it is unset. |
| 99 | func (e *Env) Duration(key string, def time.Duration) time.Duration { |
| 100 | return Parse(e, key, def, time.ParseDuration) |
| 101 | } |