From 7bf1e01decc7e16c3a2d2388515e374d9629fd79 Mon Sep 17 00:00:00 2001 From: Calle Pettersson Date: Sun, 19 Jun 2022 18:38:52 +0200 Subject: [PATCH] fix: change flag parse error handling to return errors instead of exiting (#107) * fix: change flag parse error handling to return errors instead of exiting Having ExitOnError in combination with SetOutput to a buffer instead of stdout/stderr means flags.Parse output is swallowed and kubeconform silently exits directly with exit code 2 instead of returning the error. Setting ContinueOnError instead returns the error, and writes usage help to the buffer, so error handling code in main is reached. * Add test for parsing incorrect flags Co-authored-by: Yann Hamon --- acceptance.bats | 6 ++++++ pkg/config/config.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/acceptance.bats b/acceptance.bats index bc8902e..f8ec232 100755 --- a/acceptance.bats +++ b/acceptance.bats @@ -11,6 +11,12 @@ resetCacheFolder() { [ "${lines[0]}" == 'Usage: bin/kubeconform [OPTION]... [FILE OR FOLDER]...' ] } +@test "Fail and display help when using an incorrect flag" { + run bin/kubeconform -xyz + [ "$status" -eq 1 ] + [ "${lines[0]}" == 'flag provided but not defined: -xyz' ] +} + @test "Pass when parsing a valid Kubernetes config YAML file" { run bin/kubeconform -summary fixtures/valid.yaml [ "$status" -eq 0 ] diff --git a/pkg/config/config.go b/pkg/config/config.go index 02a4dac..c0c374e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -56,7 +56,7 @@ func splitCSV(csvStr string) map[string]struct{} { func FromFlags(progName string, args []string) (Config, string, error) { var schemaLocationsParam, ignoreFilenamePatterns arrayParam var skipKindsCSV, rejectKindsCSV string - flags := flag.NewFlagSet(progName, flag.ExitOnError) + flags := flag.NewFlagSet(progName, flag.ContinueOnError) var buf bytes.Buffer flags.SetOutput(&buf)