complete nested config files

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-11-19 18:15:19 +03:00
parent ee4c343dee
commit 3398ee60f3
2 changed files with 35 additions and 5 deletions

View File

@ -65,17 +65,24 @@ func main() {
}
meters["default"] = m
l.Info(ctx, "try to parse config.yaml")
f, err := os.Open("config.yaml")
if err != nil {
l.Fatal(ctx, "failed to open config", err)
}
defer f.Close()
cfg := &config.Config{}
if err = cfg.Parse(f); err != nil {
if err = config.Parse(f, cfg); err != nil {
f.Close()
l.Fatal(ctx, "failed to open config", err)
}
f.Close()
if cfg.App.ChecksDir != "" {
var configFiles []string
if !filepath.IsAbs(cfg.App.ChecksDir) {
dir, _ := os.Getwd()
cfg.App.ChecksDir = filepath.Clean(filepath.Join(dir, cfg.App.ChecksDir))
@ -83,17 +90,40 @@ func main() {
err = filepath.WalkDir(cfg.App.ChecksDir, func(path string, info fs.DirEntry, err error) error {
if err != nil {
// fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if info.IsDir() {
if info.IsDir() || !info.Type().IsRegular() {
return nil
}
if filepath.Ext(info.Name()) != ".yaml" {
return nil
}
configFiles = append(configFiles, path)
return nil
})
if err != nil {
l.Fatal(ctx, fmt.Sprintf("error loading config: %s", cfg.App.ChecksDir), err)
}
for _, configFile := range configFiles {
l.Info(ctx, "try to parse "+configFile)
f, err := os.Open(configFile)
if err != nil {
l.Fatal(ctx, "failed to open config", err)
}
checks := []*config.CheckConfig{}
if err = config.Parse(f, &checks); err != nil {
f.Close()
l.Fatal(ctx, "failed to open config", err)
}
f.Close()
cfg.App.Checks = append(cfg.App.Checks, checks...)
}
}
if !cfg.App.MultiUser {

View File

@ -66,7 +66,7 @@ type TaskConfig struct {
Active bool `json:"active,omitempty" yaml:"active,omitempty"`
}
func (cfg *Config) Parse(r io.Reader) error {
func Parse(r io.Reader, cfg interface{}) error {
buf, err := io.ReadAll(r)
if err != nil {
return err