From 3db160ddfd880059dea47e26711857f5493d2412 Mon Sep 17 00:00:00 2001 From: Sam Tholiya Date: Fri, 27 Dec 2024 14:38:27 +0100 Subject: [PATCH] refactored code --- cmd/editor_config.go | 23 ++++++++++++++++++----- tests/test_cases.yaml | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cmd/editor_config.go b/cmd/editor_config.go index 646caffd4..07ff2be7a 100644 --- a/cmd/editor_config.go +++ b/cmd/editor_config.go @@ -15,7 +15,7 @@ import ( ) var ( - editorConfigVersion = "v3.0.0" + editorConfigVersion = "v3.0.3" defaultConfigFilePath = ".ecrc" currentConfig *config.Config cliConfig config.Config @@ -49,7 +49,10 @@ func initializeConfig() { os.Exit(1) } - _ = currentConfig.Parse() + if err := currentConfig.Parse(); err != nil { + u.LogError(atmosConfig, fmt.Errorf("failed to parse config: %w", err)) + os.Exit(1) + } if tmpExclude != "" { currentConfig.Exclude = append(currentConfig.Exclude, tmpExclude) @@ -64,9 +67,8 @@ func runMainLogic() { u.LogDebug(atmosConfig, config.GetAsString()) u.LogTrace(atmosConfig, fmt.Sprintf("Exclude Regexp: %s", config.GetExcludesAsRegularExpression())) - if utils.FileExists(config.Path) && config.Version != "" && config.Version != editorConfigVersion { - u.LogError(atmosConfig, fmt.Errorf("Version from config file is not the same as the version of the binary")) - u.LogError(atmosConfig, fmt.Errorf("Binary: %s, Config: %s", editorConfigVersion, config.Version)) + if err := checkVersion(config); err != nil { + u.LogError(atmosConfig, err) os.Exit(1) } @@ -100,6 +102,17 @@ func runMainLogic() { u.LogInfo(atmosConfig, "No errors found") } +func checkVersion(config config.Config) error { + if !utils.FileExists(config.Path) || config.Version == "" { + return nil + } + if config.Version != editorConfigVersion { + return fmt.Errorf("version mismatch: binary=%s, config=%s", + editorConfigVersion, config.Version) + } + return nil +} + // handleReturnableFlags handles early termination flags func handleReturnableFlags(config config.Config) bool { if config.ShowVersion { diff --git a/tests/test_cases.yaml b/tests/test_cases.yaml index d43c8aa73..5dfe49b5d 100644 --- a/tests/test_cases.yaml +++ b/tests/test_cases.yaml @@ -146,7 +146,7 @@ tests: - "editorconfig-checker" expect: stdout: - - "EditorConfig Checker CLI Version: v3.0.0" + - "EditorConfig Checker CLI Version: v3.0.3" - "No errors found" stderr: - "^$"