-
Notifications
You must be signed in to change notification settings - Fork 1
/
tomlenv.go
65 lines (50 loc) · 1.25 KB
/
tomlenv.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
package tomlenv
import (
"fmt"
"os"
"github.com/pelletier/go-toml"
)
// Load the default "env.toml" into your environment
func Load() error {
err := LoadFile("env.toml")
return err
}
// LoadFile wil load one or multiple files into your environment
func LoadFile(filenames ...string) error {
if len(filenames) == 0 {
return fmt.Errorf("You must provide atleast one filename.")
}
for _, filename := range filenames {
config, err := toml.LoadFile(filename)
if err != nil {
panic(fmt.Sprintf("Can't read environment variables from %s file.", filename))
}
loadInEnvironment(config)
}
return nil
}
// LoadString will load environment in based on a given TOML string
func LoadString(content string) error {
config, err := toml.Load(content)
if err != nil {
return err
}
loadInEnvironment(config)
return nil
}
func loadInEnvironment(config *toml.Tree) {
configMap := config.ToMap()
for i := range configMap {
if _, ok := configMap[i].(map[string]interface{}); ok {
configMapValue := configMap[i].(map[string]interface{})
for j := range configMapValue {
_ = os.Setenv(
fmt.Sprintf("%s.%v", i, j),
fmt.Sprintf("%v", configMapValue[j]),
)
}
continue
}
_ = os.Setenv(i, fmt.Sprintf("%v", configMap[i]))
}
}