mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 19:17:32 +00:00
add tests for config
This commit is contained in:
@@ -7,31 +7,21 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/ghodss/yaml"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
)
|
||||
|
||||
type Reader interface {
|
||||
Unmarshal(data []byte, o interface{}) error
|
||||
}
|
||||
|
||||
type ValidatableConfiguration interface {
|
||||
Validate() error
|
||||
}
|
||||
|
||||
type ReaderFunc func(data []byte, o interface{}) error
|
||||
|
||||
func (c ReaderFunc) Unmarshal(data []byte, o interface{}) error {
|
||||
return c(data, o)
|
||||
}
|
||||
|
||||
var (
|
||||
JSONReader = ReaderFunc(json.Unmarshal)
|
||||
TOMLReader = ReaderFunc(toml.Unmarshal)
|
||||
YAMLReader = ReaderFunc(func(y []byte, o interface{}) error {
|
||||
return yaml.Unmarshal(y, o)
|
||||
})
|
||||
JSONReader = json.Unmarshal
|
||||
TOMLReader = toml.Unmarshal
|
||||
YAMLReader = yaml.Unmarshal
|
||||
)
|
||||
|
||||
// Read deserializes each config file to the target obj
|
||||
@@ -39,11 +29,11 @@ var (
|
||||
// env vars are replaced in the config file as well as the file path
|
||||
func Read(obj interface{}, configFiles ...string) error {
|
||||
for _, cf := range configFiles {
|
||||
configReader, err := configReaderForFile(cf)
|
||||
readerFunc, err := readerFuncForFile(cf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := readConfigFile(configReader, cf, obj); err != nil {
|
||||
if err := readConfigFile(readerFunc, cf, obj); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -57,13 +47,9 @@ func Read(obj interface{}, configFiles ...string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func readConfigFile(configReader Reader, configFile string, obj interface{}) error {
|
||||
func readConfigFile(readerFunc ReaderFunc, configFile string, obj interface{}) error {
|
||||
configFile = os.ExpandEnv(configFile)
|
||||
|
||||
if _, err := os.Stat(configFile); err != nil {
|
||||
return errors.ThrowNotFoundf(err, "CONFI-Hs93M", "config file %s does not exist", configFile)
|
||||
}
|
||||
|
||||
configStr, err := ioutil.ReadFile(configFile)
|
||||
if err != nil {
|
||||
return errors.ThrowInternalf(err, "CONFI-nJk2a", "failed to read config file %s", configFile)
|
||||
@@ -71,14 +57,14 @@ func readConfigFile(configReader Reader, configFile string, obj interface{}) err
|
||||
|
||||
configStr = []byte(os.ExpandEnv(string(configStr)))
|
||||
|
||||
if err := configReader.Unmarshal(configStr, obj); err != nil {
|
||||
if err := readerFunc(configStr, obj); err != nil {
|
||||
return errors.ThrowInternalf(err, "CONFI-2Mc3c", "error parse config file %s", configFile)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func configReaderForFile(configFile string) (Reader, error) {
|
||||
func readerFuncForFile(configFile string) (ReaderFunc, error) {
|
||||
ext := filepath.Ext(configFile)
|
||||
switch ext {
|
||||
case ".yaml", ".yml":
|
||||
|
Reference in New Issue
Block a user