From 80237c809072ce186d61d944059a4c303a1aa5d4 Mon Sep 17 00:00:00 2001 From: RoyUP9 <87927115+RoyUP9@users.noreply.github.com> Date: Mon, 30 Aug 2021 11:43:44 +0300 Subject: [PATCH] fixed error on invalid config path (#250) --- acceptanceTests/tap_test.go | 13 ++++++++++++- cli/config/config.go | 13 ++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/acceptanceTests/tap_test.go b/acceptanceTests/tap_test.go index 8d921ed1f..e4f8888df 100644 --- a/acceptanceTests/tap_test.go +++ b/acceptanceTests/tap_test.go @@ -71,7 +71,6 @@ func TestTapAndFetch(t *testing.T) { } entries := requestResult.([]interface{}) - if len(entries) == 0 { return fmt.Errorf("unexpected entries result - Expected more than 0 entries") } @@ -523,6 +522,10 @@ func TestTapRedact(t *testing.T) { } entries := requestResult.([]interface{}) + if len(entries) == 0 { + return fmt.Errorf("unexpected entries result - Expected more than 0 entries") + } + firstEntry := entries[0].(map[string]interface{}) entryUrl := fmt.Sprintf("%v/api/entries/%v", apiServerUrl, firstEntry["id"]) @@ -625,6 +628,10 @@ func TestTapNoRedact(t *testing.T) { } entries := requestResult.([]interface{}) + if len(entries) == 0 { + return fmt.Errorf("unexpected entries result - Expected more than 0 entries") + } + firstEntry := entries[0].(map[string]interface{}) entryUrl := fmt.Sprintf("%v/api/entries/%v", apiServerUrl, firstEntry["id"]) @@ -727,6 +734,10 @@ func TestTapRegexMasking(t *testing.T) { } entries := requestResult.([]interface{}) + if len(entries) == 0 { + return fmt.Errorf("unexpected entries result - Expected more than 0 entries") + } + firstEntry := entries[0].(map[string]interface{}) entryUrl := fmt.Sprintf("%v/api/entries/%v", apiServerUrl, firstEntry["id"]) diff --git a/cli/config/config.go b/cli/config/config.go index ab6332b51..5b0032f6f 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -37,11 +37,14 @@ func InitConfig(cmd *cobra.Command) error { return err } - configFilePath := cmd.Flags().Lookup(ConfigFilePathCommandName).Value.String() - + configFilePathFlag := cmd.Flags().Lookup(ConfigFilePathCommandName) + configFilePath := configFilePathFlag.Value.String() if err := mergeConfigFile(configFilePath); err != nil { - return fmt.Errorf("invalid config, %w\n"+ - "you can regenerate the file by removing it (%v) and using `mizu config -r`", err, configFilePath) + _, isPathError := err.(*os.PathError) + if configFilePathFlag.Changed || !isPathError { + return fmt.Errorf("invalid config, %w\n"+ + "you can regenerate the file by removing it (%v) and using `mizu config -r`", err, configFilePath) + } } cmd.Flags().Visit(initFlag) @@ -67,7 +70,7 @@ func GetConfigWithDefaults() (string, error) { func mergeConfigFile(configFilePath string) error { reader, openErr := os.Open(configFilePath) if openErr != nil { - return nil + return openErr } buf, readErr := ioutil.ReadAll(reader)