Skip to content

Commit

Permalink
feat(config): add -exact-config command line argument (go-graphite#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchrome authored Feb 6, 2024
1 parent ec51e46 commit e069b71
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
15 changes: 11 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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()
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1072,7 +1072,7 @@ func TestGetQueryParamBroken(t *testing.T) {
},
]`)

_, _, err := Unmarshal(config)
_, _, err := Unmarshal(config, false)
assert.Error(t, err)

config =
Expand All @@ -1087,7 +1087,7 @@ func TestGetQueryParamBroken(t *testing.T) {
},
]`)

_, _, err = Unmarshal(config)
_, _, err = Unmarshal(config, false)
assert.Error(t, err)
}

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion graphite-clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit e069b71

Please sign in to comment.