mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
f680dd934d
* chore: rename package errors to zerrors * rename package errors to gerrors * fix error related linting issues * fix zitadel error assertion * fix gosimple linting issues * fix deprecated linting issues * resolve gci linting issues * fix import structure --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
"sigs.k8s.io/yaml"
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
type ValidatableConfiguration interface {
|
|
Validate() error
|
|
}
|
|
|
|
type ReaderFunc func(data []byte, o interface{}) error
|
|
|
|
var (
|
|
JSONReader = json.Unmarshal
|
|
TOMLReader = toml.Unmarshal
|
|
YAMLReader = func(data []byte, o interface{}) error { return yaml.Unmarshal(data, o) }
|
|
)
|
|
|
|
// Read deserializes each config file to the target obj
|
|
// using a Reader (depending on file extension)
|
|
// 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 {
|
|
readerFunc, err := readerFuncForFile(cf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := readConfigFile(readerFunc, cf, obj); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if validatable, ok := obj.(ValidatableConfiguration); ok {
|
|
if err := validatable.Validate(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func readConfigFile(readerFunc ReaderFunc, configFile string, obj interface{}) error {
|
|
configFile = os.ExpandEnv(configFile)
|
|
|
|
configStr, err := ioutil.ReadFile(configFile)
|
|
if err != nil {
|
|
return zerrors.ThrowInternalf(err, "CONFI-nJk2a", "failed to read config file %s", configFile)
|
|
}
|
|
|
|
configStr = []byte(os.ExpandEnv(string(configStr)))
|
|
|
|
if err := readerFunc(configStr, obj); err != nil {
|
|
return zerrors.ThrowInternalf(err, "CONFI-2Mc3c", "error parse config file %s", configFile)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func readerFuncForFile(configFile string) (ReaderFunc, error) {
|
|
ext := filepath.Ext(configFile)
|
|
switch ext {
|
|
case ".yaml", ".yml":
|
|
return YAMLReader, nil
|
|
case ".json":
|
|
return JSONReader, nil
|
|
case ".toml":
|
|
return TOMLReader, nil
|
|
}
|
|
return nil, zerrors.ThrowUnimplementedf(nil, "CONFI-ZLk4u", "file extension (%s) not supported", ext)
|
|
}
|