From 3398ee60f366ed8d739797da9f1a132df2f44d51 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 19 Nov 2024 18:15:19 +0300 Subject: [PATCH] complete nested config files Signed-off-by: Vasiliy Tolstov --- cmd/servicechecker/main.go | 38 ++++++++++++++++++++++++++++++++++---- pkg/config/config.go | 2 +- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/cmd/servicechecker/main.go b/cmd/servicechecker/main.go index 57d34b4..4e2ac34 100644 --- a/cmd/servicechecker/main.go +++ b/cmd/servicechecker/main.go @@ -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 { diff --git a/pkg/config/config.go b/pkg/config/config.go index c776dd6..92640ee 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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