-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathenv.go
65 lines (54 loc) · 1.62 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2025 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
// Package env loads configuration from environment variables.
//
// Env loads environment variables whose names starts with the given prefix
// and returns them as a nested map[string]any.
// Environment variables with empty values are treated as unset.
//
// It splits the names by delimiter. For example, with the default delimiter "_",
// the environment variable `PARENT_CHILD_KEY="1"` is loaded as `{PARENT: {CHILD: {KEY: "1"}}}`.
package env
import (
"os"
"strings"
"github.com/nil-go/konf/internal/maps"
)
// Env is a Provider that loads configuration from environment variables.
//
// To create a new Env, call New.
type Env struct {
prefix string
splitter func(string) []string
}
// New creates an Env with the given Option(s).
func New(opts ...Option) Env {
option := &options{}
for _, opt := range opts {
opt(option)
}
return Env(*option)
}
func (e Env) Load() (map[string]any, error) {
splitter := e.splitter
if splitter == nil {
splitter = func(s string) []string { return strings.Split(s, "_") }
}
values := make(map[string]any)
for _, env := range os.Environ() {
if e.prefix == "" || strings.HasPrefix(env, e.prefix) {
key, value, _ := strings.Cut(env, "=")
if value == "" {
// The environment variable with empty value is treated as unset.
continue
}
if keys := splitter(key); len(keys) > 1 || len(keys) == 1 && keys[0] != "" {
maps.Insert(values, keys, value)
}
}
}
return values, nil
}
func (e Env) String() string {
return "env:" + e.prefix + "*"
}