Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/kepler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ func parseArgsAndConfig() (*config.Config, error) {
return nil, err
}

// Validate config before run
if err := cfg.Validate(); err != nil {
logger.Error("Error validate command line flags", "error", err.Error())
return nil, err
}

Comment on lines +108 to +113
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not make it the responsibility of the caller to validate the config.

return cfg, nil
}

Expand Down
10 changes: 3 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ func Load(r io.Reader) (*Config, error) {
}
cfg.sanitize()

if err := cfg.Validate(); err != nil {
return nil, err
}

Comment on lines -189 to -192
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Load should validate

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

return cfg, nil
}

Expand Down Expand Up @@ -237,8 +233,8 @@ func RegisterFlags(app *kingpin.Application) ConfigUpdaterFn {
logLevel := app.Flag(LogLevelFlag, "Logging level: debug, info, warn, error").Default("info").Enum("debug", "info", "warn", "error")
logFormat := app.Flag(LogFormatFlag, "Logging format: text or json").Default("text").Enum("text", "json")
// host
hostSysFS := app.Flag(HostSysFSFlag, "Host sysfs path").Default("/sys").ExistingDir()
hostProcFS := app.Flag(HostProcFSFlag, "Host procfs path").Default("/proc").ExistingDir()
Comment on lines -240 to -241
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not make it String since we must ensure the that the directory exists

hostSysFS := app.Flag(HostSysFSFlag, "Host sysfs path").Default("/sys").String()
hostProcFS := app.Flag(HostProcFSFlag, "Host procfs path").Default("/proc").String()

// monitor
monitorInterval := app.Flag(MonitorIntervalFlag,
Expand Down Expand Up @@ -308,7 +304,7 @@ func RegisterFlags(app *kingpin.Application) ConfigUpdaterFn {
}

cfg.sanitize()
return cfg.Validate()
return nil
}
}

Expand Down
7 changes: 4 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
`
reader := strings.NewReader(yamlData)
cfg, err := Load(reader)
assert.Error(t, err)
assert.NoError(t, err)
err = cfg.Validate()
assert.Contains(t, err.Error(), "invalid configuration")
assert.Nil(t, cfg)
}

func TestCommandLinePrecedence(t *testing.T) {
Expand Down Expand Up @@ -359,7 +359,8 @@
_, parseErr := app.Parse(tc.args)
assert.Error(t, parseErr, "expected test args to produce a parse error")
cfg := DefaultConfig()
err := updateConfig(cfg)
updateConfig(cfg)

Check failure on line 362 in config/config_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
err := cfg.Validate()
assert.Error(t, err, "invalid input should be rejected by validation")
assert.Contains(t, err.Error(), tc.expectedError)
})
Expand Down
Loading