/
env.go
47 lines (43 loc) · 1.05 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
// SPDX-FileCopyrightText: 2019 Elasticsearch B.V.
// SPDX-FileCopyrightText: 2019-2023 Thibault NORMAND <me@zenithar.org>
//
// SPDX-License-Identifier: Apache-2.0 AND MIT
package flags
import (
"fmt"
"strconv"
"strings"
"github.com/fatih/structs"
"zntr.io/harp/v2/pkg/sdk/log"
)
// AsEnvVariables sets struct values from environment variables.
func AsEnvVariables(o interface{}, prefix string, skipCommented bool) map[string]string {
r := map[string]string{}
prefix = strings.ToUpper(prefix)
delim := "_"
if prefix == "" {
delim = ""
}
fields := structs.Fields(o)
for _, f := range fields {
if skipCommented {
tag := f.Tag("commented")
if tag != "" {
commented, err := strconv.ParseBool(tag)
log.CheckErr("Unable to parse tag value", err)
if commented {
continue
}
}
}
if structs.IsStruct(f.Value()) {
rf := AsEnvVariables(f.Value(), prefix+delim+f.Name(), skipCommented)
for k, v := range rf {
r[k] = v
}
} else {
r[prefix+"_"+strings.ToUpper(f.Name())] = fmt.Sprintf("%v", f.Value())
}
}
return r
}