Debian Codegraph @

providers/env/env.go

raw
1 // Package env implements a koanf.Provider that reads environment
2 // variables as conf maps.
3 package env
4
5 import (
6 "errors"
7 "os"
8 "strings"
9
10 "github.com/knadh/koanf/maps"
11 )
12
13 // Env implements an environment variables provider.
14 type Env struct {
15 prefix string
16 delim string
17 transform func(key string, value string) (string, any)
18 environ func() []string
19 }
20
21 // Opt represents optional configuration passed to the provider.
22 type Opt struct {
23 // If specified (case-sensitive), only env vars beginning with
24 // the prefix are captured. eg: "APP_"
25 Prefix string
26
27 // TransformFunc is an optional callback that takes an environment
28 // variable's string name and value, runs arbitrary transformations
29 // on them and returns a transformed string key and value of any type.
30 // Common usecase are stripping prefixes from keys, lowercasing variable names,
31 // replacing _ with . etc. Eg: APP_DB_HOST -> db.host
32 // If the returned variable name is an empty string (""), it is ignored altogether.
33 TransformFunc func(k, v string) (string, any)
34
35 // EnvironFunc is the optional function that provides the environment
36 // variables to the provider. If it's not set, os.Environ is used.
37 // This can be used to inject environment variables in tests and mocks.
38 EnvironFunc func() []string
39 }
40
41 // Provider returns an environment variables provider that returns
42 // a nested map[string]any of environment variable where the
43 // nesting hierarchy of keys is defined by delim. For instance, the
44 // delim "." will convert the key `parent.child.key: 1`
45 // to `{parent: {child: {key: 1}}}`.
46 //
47 // It takes an optional Opt argument containing a function to override
48 // the default source for environment variables, which can be useful
49 // for mocking and parallel unit tests.
50 func Provider(delim string, o Opt) *Env {
51 e := &Env{
52 delim: delim,
53 prefix: o.Prefix,
54 environ: o.EnvironFunc,
55 transform: o.TransformFunc,
56 }
57
58 // No environ function provided, use the default os.Environ.
59 if e.environ == nil {
60 e.environ = os.Environ
61 }
62
63 return e
64 }
65
66 // ReadBytes is not supported by the env provider.
67 func (e *Env) ReadBytes() ([]byte, error) {
68 return nil, errors.New("env provider does not support this method")
69 }
70
71 // Read reads all available environment variables into a key:value map
72 // and returns it.
73 func (e *Env) Read() (map[string]any, error) {
74 // Collect the environment variable keys.
75 var keys []string
76 for _, k := range e.environ() {
77 if e.prefix != "" {
78 if strings.HasPrefix(k, e.prefix) {
79 keys = append(keys, k)
80 }
81 } else {
82 keys = append(keys, k)
83 }
84 }
85
86 mp := make(map[string]any)
87 for _, k := range keys {
88 parts := strings.SplitN(k, "=", 2)
89
90 // If there's a transformation callback,
91 // run it through every key/value.
92 if e.transform != nil {
93 key, value := e.transform(parts[0], parts[1])
94 // If the callback blanked the key, omit it.
95 if key == "" {
96 continue
97 }
98
99 mp[key] = value
100 } else {
101 mp[parts[0]] = parts[1]
102 }
103
104 }
105
106 if e.delim != "" {
107 return maps.Unflatten(mp, e.delim), nil
108 }
109
110 return mp, nil
111 }