Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit cbdd5f2

Browse files
committed
moved env struct applier to a new package
1 parent 679c033 commit cbdd5f2

File tree

1 file changed

+2
-48
lines changed

1 file changed

+2
-48
lines changed

util/config.go

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"io/ioutil"
55
"os"
66
"path/filepath"
7-
"reflect"
8-
"strconv"
97

8+
envstruct "github.com/maxibanki/golang-env-struct"
109
"github.com/sirupsen/logrus"
1110

1211
"github.com/pkg/errors"
@@ -53,7 +52,7 @@ func ReadInConfig() error {
5352
} else {
5453
logrus.Info("No configuration file found, using defaults with environment variable overrides.")
5554
}
56-
if err := config.applyEnvironmentConfig(); err != nil {
55+
if err := envstruct.ApplyEnvVars(&config, "GUS"); err != nil {
5756
return errors.Wrap(err, "could not apply environment configuration")
5857
}
5958
config.DataDir, err = filepath.Abs(config.DataDir)
@@ -68,51 +67,6 @@ func ReadInConfig() error {
6867
return nil
6968
}
7069

71-
func (c *Configuration) applyEnvironmentConfig() error {
72-
return c.setDefaultValue(reflect.ValueOf(c), reflect.TypeOf(*c), -1, "GUS")
73-
}
74-
75-
func (c *Configuration) setDefaultValue(v reflect.Value, t reflect.Type, counter int, prefix string) error {
76-
if v.Kind() != reflect.Ptr {
77-
return errors.New("Not a pointer value")
78-
}
79-
f := reflect.StructField{}
80-
if counter != -1 {
81-
f = t.Field(counter)
82-
}
83-
v = reflect.Indirect(v)
84-
fieldEnv, exists := f.Tag.Lookup("env")
85-
env := os.Getenv(prefix + fieldEnv)
86-
if exists && env != "" {
87-
switch v.Kind() {
88-
case reflect.Int:
89-
envI, err := strconv.Atoi(env)
90-
if err != nil {
91-
logrus.Warningf("could not parse to int: %v", err)
92-
break
93-
}
94-
v.SetInt(int64(envI))
95-
case reflect.String:
96-
v.SetString(env)
97-
case reflect.Bool:
98-
envB, err := strconv.ParseBool(env)
99-
if err != nil {
100-
logrus.Warningf("could not parse to bool: %v", err)
101-
break
102-
}
103-
v.SetBool(envB)
104-
}
105-
}
106-
if v.Kind() == reflect.Struct {
107-
for i := 0; i < v.NumField(); i++ {
108-
if err := c.setDefaultValue(v.Field(i).Addr(), v.Type(), i, prefix+fieldEnv+"_"); err != nil {
109-
return err
110-
}
111-
}
112-
}
113-
return nil
114-
}
115-
11670
func (o oAuthConf) Enabled() bool {
11771
return o.ClientSecret != ""
11872
}

0 commit comments

Comments
 (0)