/
env.go
51 lines (45 loc) · 1.11 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
package config
import (
"fmt"
"os"
"strings"
"github.com/caarlos0/env"
iom "github.com/grokify/simplego/io/ioutilmore"
"github.com/joho/godotenv"
)
// EnvFileToJSONFile Converts an .env file to a JSON file using the definition
// provided in data.
func EnvFileToJSONFile(data interface{}, filepathENV, filepathJSON string, perm os.FileMode, prefix, indent string) error {
err := godotenv.Load(filepathENV)
if err != nil {
return err
}
err = env.Parse(data)
if err != nil {
return err
}
return iom.WriteFileJSON(filepathJSON, data, perm, prefix, indent)
}
// Return a merged environment var which is split into multiple
// vars. This is useful when the system has a size limit on
// environment variables.
func JoinEnvNumbered(prefix, delimiter string, startInt uint8, includeBase bool) string {
vals := []string{}
if includeBase {
val := os.Getenv(prefix)
if len(val) > 0 {
vals = append(vals, val)
}
}
i := startInt
for {
val := os.Getenv(fmt.Sprintf("%s_%d", prefix, i))
if len(val) > 0 {
vals = append(vals, val)
} else {
break
}
i++
}
return strings.Join(vals, delimiter)
}