-
Notifications
You must be signed in to change notification settings - Fork 0
/
abditory.go
83 lines (69 loc) · 1.79 KB
/
abditory.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
package configs
import (
"fmt"
"github.com/spf13/viper"
"github.com/ztrue/tracerr"
"log"
"reflect"
)
type Secrets struct {
DbUser string `mapstructure:"dbUser"`
DbPass string `mapstructure:"dbPass"`
SrvJwtKey string `mapstructure:"serverJwtKey"`
}
type Env struct {
DbMigrationLocation string `env:"FLAGR_MIGRATION_LOCATION"`
DbSchema string `env:"FLAGR_DB_SCHEMA"`
DbHost string `env:"FLAGR_DB_HOST"`
DbPort string `env:"FLAGR_DB_PORT"`
SrvPort string `env:"FLAGR_SERVER_PORT"`
SrvSecretsFile string `env:"FLAGR_SECRETS_FILE"`
Secrets
}
func Configure() (*Env, error) {
vipe := viper.GetViper()
env := Env{}
if err := env.RegisterEnvironmentVariables(vipe); err != nil {
return nil, tracerr.Wrap(err)
}
if err := env.LoadSecretsFile(vipe); err != nil {
return nil, tracerr.Wrap(err)
}
return &env, nil
}
func (env *Env) RegisterEnvironmentVariables(vipe *viper.Viper) error {
vipe.AutomaticEnv()
//var newEnvConfiguration Env
t := reflect.TypeOf(*env)
for i := 0; i < t.NumField(); i++ {
// Get the field, returns https://golang.org/pkg/reflect/#StructField
field := t.Field(i)
if field.Name == "Secrets" {
continue
}
// Get the field tag value
tag := field.Tag.Get("env")
if tag == "" {
continue
}
v := reflect.ValueOf(env).Elem().FieldByName(field.Name)
if v.IsValid() {
tagValue := vipe.GetString(tag)
v.Set(reflect.ValueOf(tagValue))
}
}
return nil
}
func (env *Env) LoadSecretsFile(vipe *viper.Viper) error {
vipe.SetConfigFile(env.SrvSecretsFile)
err := vipe.ReadInConfig()
if err != nil {
log.Fatal("Can't find the file .env : ", err)
}
err = vipe.Unmarshal(&env.Secrets)
if err != nil {
log.Fatal("Environment can't be loaded: ", err)
}
fmt.Printf("%+v", env)
return nil
}