From e069b71278e81a1de2b9a9c262d5d47998c40733 Mon Sep 17 00:00:00 2001 From: Artyom Antonov <64745136+mchrome@users.noreply.github.com> Date: Tue, 6 Feb 2024 13:04:11 +0500 Subject: [PATCH] feat(config): add -exact-config command line argument (#262) --- config/config.go | 15 +++++++++++---- config/config_test.go | 12 ++++++------ graphite-clickhouse.go | 3 ++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index 263c07452..8a1994eb1 100644 --- a/config/config.go +++ b/config/config.go @@ -502,7 +502,7 @@ func PrintDefaultConfig() error { } // ReadConfig reads the content of the file with given name and process it to the *Config -func ReadConfig(filename string) (*Config, []zap.Field, error) { +func ReadConfig(filename string, exactConfig bool) (*Config, []zap.Field, error) { var err error var body []byte if filename != "" { @@ -512,11 +512,11 @@ func ReadConfig(filename string) (*Config, []zap.Field, error) { } } - return Unmarshal(body) + return Unmarshal(body, exactConfig) } // Unmarshal process the body to *Config -func Unmarshal(body []byte) (cfg *Config, warns []zap.Field, err error) { +func Unmarshal(body []byte, exactConfig bool) (cfg *Config, warns []zap.Field, err error) { deprecations := make(map[string]error) cfg = New() @@ -529,9 +529,16 @@ func Unmarshal(body []byte) (cfg *Config, warns []zap.Field, err error) { body = bytes.Replace(body, []byte("[logging]"), []byte("[[logging]]"), 1) } } - if err = toml.Unmarshal(body, cfg); err != nil { + + decoder := toml.NewDecoder(bytes.NewReader(body)) + decoder.Strict(exactConfig) + + err := decoder.Decode(cfg) + + if err != nil { return nil, nil, err } + } if cfg.Logging == nil { diff --git a/config/config_test.go b/config/config_test.go index fa995b6d2..7e5724586 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -320,7 +320,7 @@ sample-initial = 10 sample-thereafter = 12 `, ) - config, _, err := Unmarshal(body) + config, _, err := Unmarshal(body, false) expected := New() require.NoError(t, err) @@ -554,7 +554,7 @@ sample-initial = 10 sample-thereafter = 12 `, ) - config, _, err := Unmarshal(body) + config, _, err := Unmarshal(body, false) expected := New() require.NoError(t, err) assert.NotNil(t, metrics.Graphite) @@ -868,7 +868,7 @@ sample-initial = 10 sample-thereafter = 12 `, ) - config, _, err := Unmarshal(body) + config, _, err := Unmarshal(body, false) expected := New() require.NoError(t, err) assert.NotNil(t, metrics.Graphite) @@ -1072,7 +1072,7 @@ func TestGetQueryParamBroken(t *testing.T) { }, ]`) - _, _, err := Unmarshal(config) + _, _, err := Unmarshal(config, false) assert.Error(t, err) config = @@ -1087,7 +1087,7 @@ func TestGetQueryParamBroken(t *testing.T) { }, ]`) - _, _, err = Unmarshal(config) + _, _, err = Unmarshal(config, false) assert.Error(t, err) } @@ -1250,7 +1250,7 @@ func TestGetQueryParam(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if config, _, err := Unmarshal(tt.config); err == nil { + if config, _, err := Unmarshal(tt.config, false); err == nil { for i := range config.ClickHouse.QueryParams { config.ClickHouse.QueryParams[i].Limiter = nil } diff --git a/graphite-clickhouse.go b/graphite-clickhouse.go index 8ef80d24c..2a218ae9e 100644 --- a/graphite-clickhouse.go +++ b/graphite-clickhouse.go @@ -100,6 +100,7 @@ func main() { configFile := flag.String("config", "/etc/graphite-clickhouse/graphite-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.") buildTags := flag.Bool("tags", false, "Build tags table") pprof := flag.String( "pprof", @@ -130,7 +131,7 @@ func main() { return } - cfg, warns, err := config.ReadConfig(*configFile) + cfg, warns, err := config.ReadConfig(*configFile, *exactConfig) if err != nil { log.Fatal(err) }