Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add editorconfig validation #896

Merged
merged 55 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
342dae4
Add editorconfig-checker to atmos cli
samtholiya Dec 27, 2024
667e1e5
Add positive test case for editorconfig-checker
samtholiya Dec 27, 2024
167092e
Update logger to atmos logger
samtholiya Dec 27, 2024
3db160d
refactored code
samtholiya Dec 27, 2024
80feea2
Temp atmos config fix to fix unit test
samtholiya Dec 28, 2024
d4a5ea2
Merge branch 'feature/dev-2836-implement-atmos-validate-editorconfig'…
samtholiya Dec 28, 2024
6ae752b
Fix logging in editor_config
samtholiya Dec 28, 2024
80cf34b
Merge branch 'feature/dev-2836-implement-atmos-validate-editorconfig'…
samtholiya Dec 28, 2024
97028ef
added EditorConfig Checker in Atmos config
samtholiya Dec 28, 2024
6b86d1a
Removed editor config checker version info
samtholiya Dec 28, 2024
97ce514
Added atmos.yaml schema help
samtholiya Dec 28, 2024
2a322b3
Update tests/test_cases.yaml
samtholiya Dec 28, 2024
63d44fb
try to fix windows workflow
samtholiya Dec 29, 2024
04f2364
Merge branch 'feature/dev-2836-implement-atmos-validate-editorconfig'…
samtholiya Dec 29, 2024
d7af492
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
samtholiya Dec 30, 2024
df45545
Add reason for git config before checkout step
samtholiya Dec 30, 2024
c11cad0
Merge branch 'feature/dev-2836-implement-atmos-validate-editorconfig'…
samtholiya Dec 30, 2024
64cfd52
Change to print message based on existing working
samtholiya Jan 4, 2025
e206e49
Added negative test case for editorconfig
samtholiya Jan 4, 2025
b1480a8
changed negative testcase
samtholiya Jan 5, 2025
a9b7c00
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
samtholiya Jan 5, 2025
07a81aa
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
samtholiya Jan 6, 2025
fd306d1
update editorconfig schema
samtholiya Jan 7, 2025
0fc7a86
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
samtholiya Jan 7, 2025
e05c033
Update the test cases based on new fixtures
samtholiya Jan 7, 2025
29d7eb6
fix verbose log level
samtholiya Jan 7, 2025
e4a3738
Add editor config documentation
samtholiya Jan 7, 2025
9df08b3
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
samtholiya Jan 7, 2025
9da0d5d
Update website/docs/core-concepts/validate/editorconfig.mdx
samtholiya Jan 9, 2025
6e45a63
Merge branch 'main' of https://github.com/cloudposse/atmos into featu…
samtholiya Jan 9, 2025
003f8fe
Merge branch 'feature/dev-2836-implement-atmos-validate-editorconfig'…
samtholiya Jan 9, 2025
c29f04f
Added editorconfig help page
samtholiya Jan 11, 2025
891b3e8
Merge branch 'main' of https://github.com/cloudposse/atmos into featu…
samtholiya Jan 11, 2025
9c139b0
Set git attributes
samtholiya Jan 11, 2025
ea121ea
validate.mdx to remove unwanted diff
samtholiya Jan 11, 2025
7cde931
Update website/docs/cli/commands/validate/validate-editorconfig.mdx
samtholiya Jan 11, 2025
cb42f79
Update website/docs/cli/commands/validate/validate-editorconfig.mdx
samtholiya Jan 11, 2025
313e77d
editorconfig test cases
samtholiya Jan 12, 2025
26dc038
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
samtholiya Jan 14, 2025
1590ec5
refactor file name for editorconfig
samtholiya Jan 15, 2025
7639df7
Update .github/workflows/test.yml
samtholiya Jan 15, 2025
aa4251d
Update .gitattributes
samtholiya Jan 15, 2025
72dfe12
changed editorconfig fixture directory
samtholiya Jan 15, 2025
da9030c
Merge branch 'feature/dev-2836-implement-atmos-validate-editorconfig'…
samtholiya Jan 15, 2025
5bce463
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
samtholiya Jan 15, 2025
6830790
Removed files to minimal
samtholiya Jan 15, 2025
0cdcfbe
Merge branch 'feature/dev-2836-implement-atmos-validate-editorconfig'…
samtholiya Jan 15, 2025
40e6bb5
Update tests/fixtures/scenarios/editorconfig/README.md
samtholiya Jan 15, 2025
c6d83d6
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
osterman Jan 16, 2025
57ac4c5
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
samtholiya Jan 16, 2025
237c8b5
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
samtholiya Jan 17, 2025
b3a4bcf
Merge branch 'main' into feature/dev-2836-implement-atmos-validate-ed…
samtholiya Jan 18, 2025
785394e
fix go.mod
samtholiya Jan 18, 2025
12ae675
updated golden snapshots
samtholiya Jan 18, 2025
bf67b39
updated the description
samtholiya Jan 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions cmd/editor_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package cmd
aknysh marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"os"

"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"

"github.com/editorconfig-checker/editorconfig-checker/v3/pkg/config"
er "github.com/editorconfig-checker/editorconfig-checker/v3/pkg/error"
"github.com/editorconfig-checker/editorconfig-checker/v3/pkg/files"
"github.com/editorconfig-checker/editorconfig-checker/v3/pkg/utils"
"github.com/editorconfig-checker/editorconfig-checker/v3/pkg/validation"
"github.com/spf13/cobra"
)

var (
editorConfigVersion = "v3.0.3"
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
defaultConfigFilePath = ".ecrc"
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
currentConfig *config.Config
cliConfig config.Config
configFilePath string
tmpExclude string
)

var editorConfigCmd *cobra.Command = &cobra.Command{
Use: "editorconfig-checker",
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
Short: "EditorConfig Checker CLI",
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
Long: "A command-line tool to check your files against the EditorConfig rules",
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initializeConfig()
},
Run: func(cmd *cobra.Command, args []string) {
runMainLogic()
},
}

// initializeConfig breaks the initialization cycle by separating the config setup
func initializeConfig() {
u.LogInfo(schema.AtmosConfiguration{}, fmt.Sprintf("EditorConfig Checker CLI Version: %s", editorConfigVersion))
if configFilePath == "" {
configFilePath = defaultConfigFilePath
}

var err error
currentConfig, err = config.NewConfig(configFilePath)
if err != nil {
u.LogError(atmosConfig, err)
os.Exit(1)
}

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)
}

currentConfig.Merge(cliConfig)
}

// runMainLogic contains the main logic
func runMainLogic() {
config := *currentConfig
u.LogDebug(atmosConfig, config.GetAsString())
u.LogTrace(atmosConfig, fmt.Sprintf("Exclude Regexp: %s", config.GetExcludesAsRegularExpression()))

if err := checkVersion(config); err != nil {
u.LogError(atmosConfig, err)
os.Exit(1)
}

if handleReturnableFlags(config) {
return
}

filePaths, err := files.GetFiles(config)
if err != nil {
u.LogError(atmosConfig, err)
os.Exit(1)
}

if config.DryRun {
for _, file := range filePaths {
u.LogInfo(atmosConfig, file)
}
os.Exit(0)
}

errors := validation.ProcessValidation(filePaths, config)
errorCount := er.GetErrorCount(errors)

if errorCount != 0 {
er.PrintErrors(errors, config)
u.LogError(atmosConfig, fmt.Errorf("\n%d errors found", errorCount))
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(1)
}

u.LogTrace(atmosConfig, fmt.Sprintf("%d files checked", len(filePaths)))
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
u.LogInfo(schema.AtmosConfiguration{}, "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 {
config.Logger.Output(editorConfigVersion)
return true
}
if config.Help {
config.Logger.Output("USAGE:")
return true
}
return false
}

// addPersistentFlags adds flags to the root command
func addPersistentFlags(cmd *cobra.Command) {
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
cmd.PersistentFlags().StringVar(&configFilePath, "config", "", "Path to the configuration file")
cmd.PersistentFlags().StringVar(&tmpExclude, "exclude", "", "Regex to exclude files from checking")
cmd.PersistentFlags().BoolVar(&cliConfig.IgnoreDefaults, "ignore-defaults", false, "Ignore default excludes")
cmd.PersistentFlags().BoolVar(&cliConfig.DryRun, "dry-run", false, "Show which files would be checked")
cmd.PersistentFlags().BoolVar(&cliConfig.ShowVersion, "version", false, "Print the version number")
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
cmd.PersistentFlags().BoolVar(&cliConfig.Help, "help", false, "Print help information")
cmd.PersistentFlags().StringVar(&cliConfig.Format, "format", "default", "Specify the output format: default, gcc")
cmd.PersistentFlags().BoolVar(&cliConfig.Verbose, "verbose", false, "Print debugging information")
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
cmd.PersistentFlags().BoolVar(&cliConfig.NoColor, "no-color", false, "Don't print colors")
cmd.PersistentFlags().BoolVar(&cliConfig.Disable.TrimTrailingWhitespace, "disable-trim-trailing-whitespace", false, "Disable trailing whitespace check")
cmd.PersistentFlags().BoolVar(&cliConfig.Disable.EndOfLine, "disable-end-of-line", false, "Disable end-of-line check")
cmd.PersistentFlags().BoolVar(&cliConfig.Disable.InsertFinalNewline, "disable-insert-final-newline", false, "Disable final newline check")
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")
}
samtholiya marked this conversation as resolved.
Show resolved Hide resolved

func init() {
// Add flags
addPersistentFlags(editorConfigCmd)
// Add command
validateCmd.AddCommand(editorConfigCmd)
}
38 changes: 38 additions & 0 deletions examples/demo-stacks/.ecrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# EditorConfig for Terraform repository
samtholiya marked this conversation as resolved.
Show resolved Hide resolved

root = true

# Terraform files
[*.tf]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# HCL files (for Terraform configurations)
[*.hcl]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# Atmos configurations (if they exist as YAML or TOML)
[*.yml]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.toml]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
4 changes: 4 additions & 0 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion tests/test_cases.yaml
aknysh marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,21 @@ tests:
stderr:
- 'unknown command "non-existent" for "atmos"'
exit_code: 1

- name: atmos validate editorconfig-check
samtholiya marked this conversation as resolved.
Show resolved Hide resolved
enabled: true
description: "Ensure atmos CLI validates based on the .ecrc file."
workdir: "../examples/demo-stacks/"
command: "atmos"
args:
- "validate"
- "editorconfig-checker"
expect:
stdout:
- "EditorConfig Checker CLI Version: v3.0.3"
osterman marked this conversation as resolved.
Show resolved Hide resolved
- "No errors found"
stderr:
- "^$"
exit_code: 0
- name: atmos describe config -f yaml
enabled: true
description: "Ensure atmos CLI outputs the Atmos configuration in YAML."
Expand Down
Loading