From 3f4e51e740ca5ec8805d34d4d9cdaac36a5b6d25 Mon Sep 17 00:00:00 2001 From: Artyom Antonov <64745136+mchrome@users.noreply.github.com> Date: Tue, 6 Feb 2024 14:07:31 +0500 Subject: [PATCH] feat(config): add -exact-config command line argument (#139) --- carbon-clickhouse.go | 3 ++- carbon/app.go | 8 ++++---- carbon/config.go | 13 +++++++++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/carbon-clickhouse.go b/carbon-clickhouse.go index c7298a89..75cfed87 100644 --- a/carbon-clickhouse.go +++ b/carbon-clickhouse.go @@ -47,6 +47,7 @@ func main() { configFile := flag.String("config", "/etc/carbon-clickhouse/carbon-clickhouse.conf", "Filename of config") printDefaultConfig := flag.Bool("config-print-default", false, "Print default config") checkConfig := flag.Bool("check-config", false, "Check config and exit") + exactConfig := flag.Bool("exact-config", false, "Ensure that all config params are contained in the target struct.") printVersion := flag.Bool("version", false, "Print version") cat := flag.String("cat", "", "Print RowBinary file in TabSeparated format") bincat := flag.String("recover", "", "Read all good records from corrupted data file. Write binary data to stdout") @@ -102,7 +103,7 @@ func main() { app := carbon.New(*configFile) - if err = app.ParseConfig(); err != nil { + if err = app.ParseConfig(*exactConfig); err != nil { log.Fatal(err) } diff --git a/carbon/app.go b/carbon/app.go index 341345d6..bf649e85 100644 --- a/carbon/app.go +++ b/carbon/app.go @@ -47,8 +47,8 @@ func New(configFilename string) *App { } // configure loads config from config file, schemas.conf, aggregation.conf -func (app *App) configure() error { - cfg, err := ReadConfig(app.ConfigFilename) +func (app *App) configure(exactConfig bool) error { + cfg, err := ReadConfig(app.ConfigFilename, exactConfig) if err != nil { return err } @@ -88,11 +88,11 @@ func (app *App) configure() error { } // ParseConfig loads config from config file -func (app *App) ParseConfig() error { +func (app *App) ParseConfig(exactConfig bool) error { app.Lock() defer app.Unlock() - return app.configure() + return app.configure(exactConfig) } // Stop all socket listeners diff --git a/carbon/config.go b/carbon/config.go index f14a4451..7b3a2fc9 100644 --- a/carbon/config.go +++ b/carbon/config.go @@ -250,7 +250,7 @@ func PrintDefaultConfig() error { } // ReadConfig ... -func ReadConfig(filename string) (*Config, error) { +func ReadConfig(filename string, exactConfig bool) (*Config, error) { var err error cfg := NewConfig() @@ -265,9 +265,18 @@ func ReadConfig(filename string) (*Config, error) { // @TODO: fix for config starts with [logging] body = strings.Replace(body, "\n[logging]\n", "\n[[logging]]\n", -1) - if _, err := toml.Decode(body, cfg); err != nil { + md, err := toml.Decode(body, cfg) + + if err != nil { return nil, err } + + if exactConfig { + undecoded := md.Undecoded() + if len(undecoded) > 0 { + return nil, fmt.Errorf("Config file (%s) contains unknown keys: %q", filename, undecoded) + } + } } if cfg.Logging == nil {