From b2f1cef335eff235394de336593687236a3b88bb Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 23 Jun 2024 23:33:58 +0200 Subject: [PATCH 1/2] cli: add check config flag --- cmds/hound/main.go | 5 +++++ cmds/houndd/main.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/cmds/hound/main.go b/cmds/hound/main.go index 9f947fa3..7c81757b 100644 --- a/cmds/hound/main.go +++ b/cmds/hound/main.go @@ -88,6 +88,7 @@ func main() { flagCase := flag.Bool("ignore-case", false, "") flagStats := flag.Bool("show-stats", false, "") flagGrep := flag.Bool("like-grep", false, "") + flagCheckCfg := flag.Bool("check-config", false, "") flag.Parse() @@ -113,6 +114,10 @@ func main() { log.Panic(err) } + if *flagCheckCfg { + return + } + res, repos, err := client.SearchAndLoadRepos(&cfg, flag.Arg(0), *flagRepos, diff --git a/cmds/houndd/main.go b/cmds/houndd/main.go index eb8038a2..79e7f131 100644 --- a/cmds/houndd/main.go +++ b/cmds/houndd/main.go @@ -130,6 +130,7 @@ func main() { error_log = log.New(os.Stderr, "", log.LstdFlags) flagConf := flag.String("conf", "config.json", "") + flagCheckConf := flag.Bool("check-conf", false, "") flagAddr := flag.String("addr", ":6080", "") flagDev := flag.Bool("dev", false, "") flagVer := flag.Bool("version", false, "Display version and exit") @@ -146,6 +147,10 @@ func main() { panic(err) } + if *flagCheckConf { + return + } + // Start the web server on a background routine. ws := web.Start(&cfg, *flagAddr, *flagDev) From f917a457570ad8659d02fca4311cc91cadcadc00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Mon, 24 Jun 2024 22:11:15 +0200 Subject: [PATCH 2/2] Set DisallowUnknownFields when config is checked --- cmds/houndd/main.go | 10 +++++----- config/config.go | 9 +++++++-- config/config_test.go | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/cmds/houndd/main.go b/cmds/houndd/main.go index 79e7f131..53b265b1 100644 --- a/cmds/houndd/main.go +++ b/cmds/houndd/main.go @@ -143,14 +143,14 @@ func main() { } var cfg config.Config - if err := cfg.LoadFromFile(*flagConf); err != nil { + if err := cfg.LoadFromFile(*flagConf, *flagCheckConf); err != nil { panic(err) } - if *flagCheckConf { - return - } - + if *flagCheckConf { + return + } + // Start the web server on a background routine. ws := web.Start(&cfg, *flagAddr, *flagDev) diff --git a/config/config.go b/config/config.go index c978c009..9f714def 100644 --- a/config/config.go +++ b/config/config.go @@ -188,14 +188,19 @@ func mergeVCSConfigs(cfg *Config) error { return nil } -func (c *Config) LoadFromFile(filename string) error { +func (c *Config) LoadFromFile(filename string, disallowUnknownFields bool) error { r, err := os.Open(filename) if err != nil { return err } defer r.Close() - if err := json.NewDecoder(r).Decode(c); err != nil { + decoder := json.NewDecoder(r) + if disallowUnknownFields { + decoder.DisallowUnknownFields() + } + + if err := decoder.Decode(c); err != nil { return err } diff --git a/config/config_test.go b/config/config_test.go index 5b3328af..dd788e0f 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -20,7 +20,7 @@ func rootDir() string { // add examples, we don't muck them up. func TestExampleConfigsAreValid(t *testing.T) { var cfg Config - if err := cfg.LoadFromFile(filepath.Join(rootDir(), exampleConfigFile)); err != nil { + if err := cfg.LoadFromFile(filepath.Join(rootDir(), exampleConfigFile), true); err != nil { t.Fatalf("Unable to parse %s: %s", exampleConfigFile, err) }