This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
114 lines (106 loc) · 2.8 KB
/
config.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package config
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"reflect"
"github.com/BurntSushi/toml"
"github.com/caarlos0/env/v6"
)
func loadDefault(defaultValues string, cfg interface{}) error {
if _, err := toml.Decode(defaultValues, cfg); err != nil {
return err
}
return nil
}
func loadEnv(cfg interface{}) error {
if err := env.Parse(cfg); err != nil {
return err
}
return nil
}
func loadFile(path string, cfg interface{}) error {
bs, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
return err
}
cfgToml := string(bs)
if _, err := toml.Decode(cfgToml, cfg); err != nil {
return err
}
return nil
}
//LoadConfig is the function that loads the configuration
func LoadConfig(filePath string, defaultValues string, cfg interface{}) error {
//Get default configuration
if err := loadDefault(defaultValues, cfg); err != nil {
return fmt.Errorf("error loading default configuration: %w", err)
}
// Get file configuration
var errLoadFile error
if filePath != "" {
errLoadFile = loadFile(filePath, cfg)
}
// Overwrite file configuration with the env configuration
errLoadEnv := loadEnv(cfg)
if errLoadFile != nil {
return fmt.Errorf("error loading configuration file: %w", errLoadFile)
}
if errLoadEnv != nil {
return fmt.Errorf("error loading environment variables: %w", errLoadEnv)
}
return nil
}
func structToMapHezNode(item interface{}, prevTags string) map[string]interface{} {
res := map[string]interface{}{}
if item == nil {
return res
}
v := reflect.TypeOf(item)
reflectValue := reflect.ValueOf(item)
reflectValue = reflect.Indirect(reflectValue)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
for i := 0; i < v.NumField(); i++ {
tag := v.Field(i).Name
tag2 := v.Field(i).Tag.Get("env")
field := reflectValue.Field(i)
if v.Field(i).Type.Kind() == reflect.Struct {
res[tag] = structToMapHezNode(field.Interface(), tag+"::"+tag2)
} else {
if !field.IsZero() {
if reflect.TypeOf(field.Interface()).String() == "time.Duration" {
res[prevTags] = reflect.ValueOf(field.Interface())
} else {
res[tag+"::"+tag2] = reflect.ValueOf(field.Interface())
}
}
}
}
for k, v := range res {
if reflect.TypeOf(v).String() == "reflect.Value" {
log.Println("############## ", k, " ==> ", v)
}
}
return res
}
func SourceParamsHezNode(filePath string, envCfg, fileCfg interface{}) error {
log.Println("ENV parameters")
errorEnv := loadEnv(envCfg)
if errorEnv != nil {
return fmt.Errorf("error reading environment variables: %w", errorEnv)
}
_ = structToMapHezNode(envCfg, "")
log.Println("File parameters")
var errorFile error
if filePath != "" {
errorFile = loadFile(filePath, fileCfg)
}
if errorFile != nil {
return fmt.Errorf("error reading file variables: %w", errorFile)
}
_ = structToMapHezNode(fileCfg, "")
return nil
}