Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
samtholiya committed Dec 27, 2024
1 parent 167092e commit 3db160d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 18 additions & 5 deletions cmd/editor_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

var (
editorConfigVersion = "v3.0.0"
editorConfigVersion = "v3.0.3"
defaultConfigFilePath = ".ecrc"
currentConfig *config.Config
cliConfig config.Config
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
- "^$"
Expand Down

0 comments on commit 3db160d

Please sign in to comment.