Skip to content

Commit

Permalink
Merge branch 'feature/dev-2836-implement-atmos-validate-editorconfig'…
Browse files Browse the repository at this point in the history
… of https://github.com/cloudposse/atmos into feature/dev-2836-implement-atmos-validate-editorconfig
  • Loading branch information
samtholiya committed Dec 28, 2024
2 parents 6ae752b + d4a5ea2 commit 80cf34b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
32 changes: 26 additions & 6 deletions cmd/editor_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

var (
editorConfigVersion = "v3.0.0"
defaultConfigFilePath = ".ecrc"
editorConfigVersion = "v3.0.3"
defaultConfigFilePath = ".editorconfig"
initEditorConfig bool
currentConfig *config.Config
cliConfig config.Config
configFilePath string
Expand Down Expand Up @@ -45,10 +46,17 @@ func initializeConfig() {
var err error
currentConfig, err = config.NewConfig(configFilePath)
if err != nil {
u.LogError(atmosConfig, err)
u.LogErrorAndExit(atmosConfig, err)
os.Exit(1)
}

if initEditorConfig {
err := currentConfig.Save(editorConfigVersion)
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
}
}

_ = currentConfig.Parse()

if tmpExclude != "" {
Expand All @@ -64,9 +72,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.LogErrorAndExit(atmosConfig, fmt.Errorf("Binary: %s, Config: %s", editorConfigVersion, config.Version))
if err := checkVersion(config); err != nil {
u.LogErrorAndExit(atmosConfig, err)
}

if handleReturnableFlags(config) {
Expand Down Expand Up @@ -97,6 +104,18 @@ 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 Expand Up @@ -127,6 +146,7 @@ func addPersistentFlags(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVar(&cliConfig.Disable.Indentation, "disable-indentation", false, "Disable indentation check")
cmd.PersistentFlags().BoolVar(&cliConfig.Disable.IndentSize, "disable-indent-size", false, "Disable indent size check")
cmd.PersistentFlags().BoolVar(&cliConfig.Disable.MaxLineLength, "disable-max-line-length", false, "Disable max line length check")
cmd.PersistentFlags().BoolVar(&initEditorConfig, "init", false, "creates an initial configuration")
}

func init() {
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"
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 80cf34b

Please sign in to comment.