From 4c32535bfab64956fcaea0018985934983bee20c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 19 Nov 2024 12:37:17 +0300 Subject: [PATCH] initial import for nested configs Signed-off-by: Vasiliy Tolstov --- cmd/servicechecker/main.go | 25 ++++++++++++++++++++++++- pkg/config/config.go | 9 +++++---- pkg/grpcconn/protoset.go | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/cmd/servicechecker/main.go b/cmd/servicechecker/main.go index 9be32d7..dce2cd1 100644 --- a/cmd/servicechecker/main.go +++ b/cmd/servicechecker/main.go @@ -3,8 +3,10 @@ package main import ( "context" "fmt" + "io/fs" "os" "os/signal" + "path/filepath" "time" openapi_v3 "github.com/google/gnostic/openapiv3" @@ -73,6 +75,27 @@ func main() { l.Fatal(ctx, "failed to open config", err) } + if cfg.App.ChecksDir != "" { + if !filepath.IsAbs(cfg.App.ChecksDir) { + dir, _ := os.Getwd() + cfg.App.ChecksDir = filepath.Clean(filepath.Join(dir, cfg.App.ChecksDir)) + } + + 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() { + return nil + } + return nil + }) + if err != nil { + l.Fatal(ctx, fmt.Sprintf("error loading config: %s", cfg.App.ChecksDir), err) + } + } + if !cfg.App.MultiUser { m.Init( meter.WriteFDMetrics(true), @@ -153,7 +176,7 @@ func main() { } clients["http"] = hcli - for _, check := range cfg.Checks { + for _, check := range cfg.App.Checks { l.Info(ctx, fmt.Sprintf("check %#+v", check)) if !check.Active { continue diff --git a/pkg/config/config.go b/pkg/config/config.go index a1c9007..c776dd6 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -8,7 +8,9 @@ import ( ) type AppConfig struct { - MultiUser bool `json:"multi_user,omitempty" yaml:"multi_user,omitempty"` + ChecksDir string `json:"checks_dir,omitempty" yaml:"checks_dir,omitempty"` + Checks []*CheckConfig `json:"checks,omitempty" yaml:"checks,omitempty"` + MultiUser bool `json:"multi_user,omitempty" yaml:"multi_user,omitempty"` } type MeterConfig struct { @@ -17,9 +19,8 @@ type MeterConfig struct { } type Config struct { - App *AppConfig `json:"app,omitempty" yaml:"app,omitempty"` - Meter *MeterConfig `json:"meter,omitempty" yaml:"meter,omitempty"` - Checks []*CheckConfig `json:"checks,omitempty" yaml:"checks,omitempty"` + App *AppConfig `json:"app,omitempty" yaml:"app,omitempty"` + Meter *MeterConfig `json:"meter,omitempty" yaml:"meter,omitempty"` } type CheckConfig struct { diff --git a/pkg/grpcconn/protoset.go b/pkg/grpcconn/protoset.go index b17b68c..c6ca835 100644 --- a/pkg/grpcconn/protoset.go +++ b/pkg/grpcconn/protoset.go @@ -3,8 +3,8 @@ package grpcconn import ( - "github.com/emicklei/proto" "github.com/jhump/protoreflect/desc" + "google.golang.org/protobuf/proto" ) var protoSets = map[string]*desc.FileDescriptor