Created
April 28, 2022 06:17
-
-
Save mehmetcemyucel/26331fd659dbb4534bfd4c160cde1d32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package config | |
import ( | |
"fmt" | |
"github.com/spf13/viper" | |
"reflect" | |
"strings" | |
) | |
var readFromEnv = func(v *viper.Viper) *viper.Viper { | |
fmt.Println("Reading environment configuration") | |
addKeysToViper(v) | |
v.AutomaticEnv() | |
return v | |
} | |
func addKeysToViper(v *viper.Viper) { | |
var reply interface{} = Config{} | |
t := reflect.TypeOf(reply) | |
keys := getAllKeys(t) | |
for _, key := range keys { | |
v.SetDefault(key, "") | |
} | |
} | |
func getAllKeys(t reflect.Type) []string { | |
var result []string | |
for i := 0; i < t.NumField(); i++ { | |
f := t.Field(i) | |
n := strings.ToUpper(f.Tag.Get(ymlTagName)) | |
if reflect.Struct == f.Type.Kind() { | |
subkeys := getAllKeys(f.Type) | |
for _, k := range subkeys { | |
result = append(result, n+"."+k) | |
} | |
} else { | |
result = append(result, n) | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment