From ad4d24763b9c05421134c88674a4aa090d1ef24f Mon Sep 17 00:00:00 2001 From: Oula Kuuva Date: Thu, 25 Sep 2025 19:35:11 +0300 Subject: [PATCH 1/4] feat: add support for .editorconfig Fixes #61. --- formatters/basic/config.go | 40 +++++++++++++++++++++++++++++++++++++- go.mod | 16 ++++++++++++--- go.sum | 7 +++++++ 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/formatters/basic/config.go b/formatters/basic/config.go index 2a32d05..dc1ca08 100644 --- a/formatters/basic/config.go +++ b/formatters/basic/config.go @@ -15,8 +15,11 @@ package basic import ( + "os" "runtime" + "strconv" + "github.com/editorconfig/editorconfig-core-go/v2" "github.com/google/yamlfmt" yamlFeatures "github.com/google/yamlfmt/formatters/basic/features" ) @@ -42,14 +45,49 @@ type Config struct { ForceArrayStyle yamlFeatures.SequenceStyle `mapstructure:"force_array_style"` } +func (c *Config) applyEditorConfig() { + cwd, err := os.Getwd() + if err != nil { + return + } + + ec, err := editorconfig.GetDefinitionForFilename(cwd) + if err != nil { + return + } + + indent, err := strconv.Atoi(ec.IndentSize) + if err == nil && indent > 0 { + c.Indent = indent + } + + if ec.EndOfLine == "crlf" { + c.LineEnding = yamlfmt.LineBreakStyleCRLF + } else if ec.EndOfLine == "lf" { + c.LineEnding = yamlfmt.LineBreakStyleLF + } else if ec.EndOfLine == "cr" { + // should we log this? + } + + if ec.TrimTrailingWhitespace != nil { + c.TrimTrailingWhitespace = *ec.TrimTrailingWhitespace + } + + if ec.InsertFinalNewline != nil { + c.EOFNewline = *ec.InsertFinalNewline + } +} + func DefaultConfig() *Config { lineBreakStyle := yamlfmt.LineBreakStyleLF if runtime.GOOS == "windows" { lineBreakStyle = yamlfmt.LineBreakStyleCRLF } - return &Config{ + config := &Config{ Indent: 2, LineEnding: lineBreakStyle, PadLineComments: 1, } + config.applyEditorConfig() + return config } diff --git a/go.mod b/go.mod index 8bc0f5b..c86272a 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,24 @@ module github.com/google/yamlfmt -go 1.21 +go 1.22.0 + +toolchain go1.24.6 require ( github.com/bmatcuk/doublestar/v4 v4.7.1 - github.com/google/go-cmp v0.6.0 + github.com/google/go-cmp v0.7.0 github.com/mitchellh/mapstructure v1.5.0 github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 ) -require golang.org/x/text v0.14.0 // indirect +require ( + golang.org/x/mod v0.23.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect +) + +require ( + github.com/editorconfig/editorconfig-core-go/v2 v2.6.3 + golang.org/x/text v0.14.0 // indirect +) diff --git a/go.sum b/go.sum index 4245d40..b5b31d7 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,11 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/editorconfig/editorconfig-core-go/v2 v2.6.3 h1:XVUp6qW3BIkmM3/1EkrHpa6bL56APOynfXcZEmIgOhs= +github.com/editorconfig/editorconfig-core-go/v2 v2.6.3/go.mod h1:ThHVc+hqbUsmE1wmK/MASpQEhCleWu1JDJDNhUOMy0c= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -17,9 +20,13 @@ github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCw github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM= +golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From ff5f8e3002d3f1d8ac2f146589b16a6764084251 Mon Sep 17 00:00:00 2001 From: Oula Kuuva Date: Thu, 25 Sep 2025 20:50:02 +0300 Subject: [PATCH 2/4] chore: go mod tidy --- go.sum | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/go.sum b/go.sum index b5b31d7..9320f05 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,7 @@ github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxK github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/editorconfig/editorconfig-core-go/v2 v2.6.3 h1:XVUp6qW3BIkmM3/1EkrHpa6bL56APOynfXcZEmIgOhs= github.com/editorconfig/editorconfig-core-go/v2 v2.6.3/go.mod h1:ThHVc+hqbUsmE1wmK/MASpQEhCleWu1JDJDNhUOMy0c= -github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= @@ -18,8 +17,9 @@ github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+e github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw= github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM= golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= From 53201424e35dde05d3f7288a06ad9f36e4082484 Mon Sep 17 00:00:00 2001 From: Oula Kuuva Date: Thu, 25 Sep 2025 21:32:37 +0300 Subject: [PATCH 3/4] test: editorconfig intergration --- b.yaml | 0 integrationtest/command/command_test.go | 24 +++++++++++++++++++ .../editorconfig_basic/after/.editorconfig | 5 ++++ .../testdata/editorconfig_basic/after/a.yaml | 21 ++++++++++++++++ .../editorconfig_basic/before/.editorconfig | 5 ++++ .../testdata/editorconfig_basic/before/a.yaml | 21 ++++++++++++++++ .../editorconfig_basic/stdout/stderr.txt | 0 .../editorconfig_basic/stdout/stdout.txt | 0 .../editorconfig_nested/after/.editorconfig | 5 ++++ .../testdata/editorconfig_nested/after/a.yaml | 21 ++++++++++++++++ .../after/foo/.editorconfig | 3 +++ .../editorconfig_nested/after/foo/b.yaml | 21 ++++++++++++++++ .../editorconfig_nested/before/.editorconfig | 5 ++++ .../editorconfig_nested/before/a.yaml | 21 ++++++++++++++++ .../before/foo/.editorconfig | 3 +++ .../editorconfig_nested/before/foo/b.yaml | 21 ++++++++++++++++ .../editorconfig_nested/stdout/stderr.txt | 0 .../editorconfig_nested/stdout/stdout.txt | 0 .../editorconfig_override/after/a.yaml | 21 ++++++++++++++++ .../before/.editorconfig | 5 ++++ .../editorconfig_override/before/.yamlfmt | 3 +++ .../editorconfig_override/before/a.yaml | 21 ++++++++++++++++ .../editorconfig_override/stdout/stderr.txt | 0 .../editorconfig_override/stdout/stdout.txt | 0 24 files changed, 226 insertions(+) create mode 100644 b.yaml create mode 100755 integrationtest/command/testdata/editorconfig_basic/after/.editorconfig create mode 100644 integrationtest/command/testdata/editorconfig_basic/after/a.yaml create mode 100644 integrationtest/command/testdata/editorconfig_basic/before/.editorconfig create mode 100644 integrationtest/command/testdata/editorconfig_basic/before/a.yaml create mode 100644 integrationtest/command/testdata/editorconfig_basic/stdout/stderr.txt create mode 100644 integrationtest/command/testdata/editorconfig_basic/stdout/stdout.txt create mode 100755 integrationtest/command/testdata/editorconfig_nested/after/.editorconfig create mode 100644 integrationtest/command/testdata/editorconfig_nested/after/a.yaml create mode 100755 integrationtest/command/testdata/editorconfig_nested/after/foo/.editorconfig create mode 100644 integrationtest/command/testdata/editorconfig_nested/after/foo/b.yaml create mode 100644 integrationtest/command/testdata/editorconfig_nested/before/.editorconfig create mode 100644 integrationtest/command/testdata/editorconfig_nested/before/a.yaml create mode 100644 integrationtest/command/testdata/editorconfig_nested/before/foo/.editorconfig create mode 100644 integrationtest/command/testdata/editorconfig_nested/before/foo/b.yaml create mode 100644 integrationtest/command/testdata/editorconfig_nested/stdout/stderr.txt create mode 100644 integrationtest/command/testdata/editorconfig_nested/stdout/stdout.txt create mode 100644 integrationtest/command/testdata/editorconfig_override/after/a.yaml create mode 100644 integrationtest/command/testdata/editorconfig_override/before/.editorconfig create mode 100644 integrationtest/command/testdata/editorconfig_override/before/.yamlfmt create mode 100644 integrationtest/command/testdata/editorconfig_override/before/a.yaml create mode 100644 integrationtest/command/testdata/editorconfig_override/stdout/stderr.txt create mode 100644 integrationtest/command/testdata/editorconfig_override/stdout/stdout.txt diff --git a/b.yaml b/b.yaml new file mode 100644 index 0000000..e69de29 diff --git a/integrationtest/command/command_test.go b/integrationtest/command/command_test.go index 81e27c0..e65c211 100644 --- a/integrationtest/command/command_test.go +++ b/integrationtest/command/command_test.go @@ -203,3 +203,27 @@ func TestDebugDiffs(t *testing.T) { Update: *updateFlag, }.Run(t) } + +func TestEditorconfigBasic(t *testing.T) { + TestCase{ + Dir: "editorconfig_basic", + Command: yamlfmtWithArgs("."), + Update: *updateFlag, + }.Run(t) +} + +func TestEditorconfigNested(t *testing.T) { + TestCase{ + Dir: "editorconfig_nested", + Command: yamlfmtWithArgs("."), + Update: *updateFlag, + }.Run(t) +} + +func TestEditorconfigOverride(t *testing.T) { + TestCase{ + Dir: "editorconfig_override", + Command: yamlfmtWithArgs("-format indent=2 ."), + Update: *updateFlag, + }.Run(t) +} diff --git a/integrationtest/command/testdata/editorconfig_basic/after/.editorconfig b/integrationtest/command/testdata/editorconfig_basic/after/.editorconfig new file mode 100755 index 0000000..c32ca47 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_basic/after/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*.yaml] +indent_size = 3 +trim_trailing_whitespace = true diff --git a/integrationtest/command/testdata/editorconfig_basic/after/a.yaml b/integrationtest/command/testdata/editorconfig_basic/after/a.yaml new file mode 100644 index 0000000..3a0fa7a --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_basic/after/a.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/integrationtest/command/testdata/editorconfig_basic/before/.editorconfig b/integrationtest/command/testdata/editorconfig_basic/before/.editorconfig new file mode 100644 index 0000000..c32ca47 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_basic/before/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*.yaml] +indent_size = 3 +trim_trailing_whitespace = true diff --git a/integrationtest/command/testdata/editorconfig_basic/before/a.yaml b/integrationtest/command/testdata/editorconfig_basic/before/a.yaml new file mode 100644 index 0000000..a9b5e35 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_basic/before/a.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/integrationtest/command/testdata/editorconfig_basic/stdout/stderr.txt b/integrationtest/command/testdata/editorconfig_basic/stdout/stderr.txt new file mode 100644 index 0000000..e69de29 diff --git a/integrationtest/command/testdata/editorconfig_basic/stdout/stdout.txt b/integrationtest/command/testdata/editorconfig_basic/stdout/stdout.txt new file mode 100644 index 0000000..e69de29 diff --git a/integrationtest/command/testdata/editorconfig_nested/after/.editorconfig b/integrationtest/command/testdata/editorconfig_nested/after/.editorconfig new file mode 100755 index 0000000..c32ca47 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_nested/after/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*.yaml] +indent_size = 3 +trim_trailing_whitespace = true diff --git a/integrationtest/command/testdata/editorconfig_nested/after/a.yaml b/integrationtest/command/testdata/editorconfig_nested/after/a.yaml new file mode 100644 index 0000000..3a0fa7a --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_nested/after/a.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/integrationtest/command/testdata/editorconfig_nested/after/foo/.editorconfig b/integrationtest/command/testdata/editorconfig_nested/after/foo/.editorconfig new file mode 100755 index 0000000..826e383 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_nested/after/foo/.editorconfig @@ -0,0 +1,3 @@ +[*.yaml] +indent_size = 4 +trim_trailing_whitespace = false diff --git a/integrationtest/command/testdata/editorconfig_nested/after/foo/b.yaml b/integrationtest/command/testdata/editorconfig_nested/after/foo/b.yaml new file mode 100644 index 0000000..7743a15 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_nested/after/foo/b.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/integrationtest/command/testdata/editorconfig_nested/before/.editorconfig b/integrationtest/command/testdata/editorconfig_nested/before/.editorconfig new file mode 100644 index 0000000..c32ca47 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_nested/before/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*.yaml] +indent_size = 3 +trim_trailing_whitespace = true diff --git a/integrationtest/command/testdata/editorconfig_nested/before/a.yaml b/integrationtest/command/testdata/editorconfig_nested/before/a.yaml new file mode 100644 index 0000000..a9b5e35 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_nested/before/a.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/integrationtest/command/testdata/editorconfig_nested/before/foo/.editorconfig b/integrationtest/command/testdata/editorconfig_nested/before/foo/.editorconfig new file mode 100644 index 0000000..826e383 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_nested/before/foo/.editorconfig @@ -0,0 +1,3 @@ +[*.yaml] +indent_size = 4 +trim_trailing_whitespace = false diff --git a/integrationtest/command/testdata/editorconfig_nested/before/foo/b.yaml b/integrationtest/command/testdata/editorconfig_nested/before/foo/b.yaml new file mode 100644 index 0000000..a9b5e35 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_nested/before/foo/b.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/integrationtest/command/testdata/editorconfig_nested/stdout/stderr.txt b/integrationtest/command/testdata/editorconfig_nested/stdout/stderr.txt new file mode 100644 index 0000000..e69de29 diff --git a/integrationtest/command/testdata/editorconfig_nested/stdout/stdout.txt b/integrationtest/command/testdata/editorconfig_nested/stdout/stdout.txt new file mode 100644 index 0000000..e69de29 diff --git a/integrationtest/command/testdata/editorconfig_override/after/a.yaml b/integrationtest/command/testdata/editorconfig_override/after/a.yaml new file mode 100644 index 0000000..007ecd3 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_override/after/a.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/integrationtest/command/testdata/editorconfig_override/before/.editorconfig b/integrationtest/command/testdata/editorconfig_override/before/.editorconfig new file mode 100644 index 0000000..c32ca47 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_override/before/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*.yaml] +indent_size = 3 +trim_trailing_whitespace = true diff --git a/integrationtest/command/testdata/editorconfig_override/before/.yamlfmt b/integrationtest/command/testdata/editorconfig_override/before/.yamlfmt new file mode 100644 index 0000000..270b30a --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_override/before/.yamlfmt @@ -0,0 +1,3 @@ +formatter: + type: basic + indent: 2 diff --git a/integrationtest/command/testdata/editorconfig_override/before/a.yaml b/integrationtest/command/testdata/editorconfig_override/before/a.yaml new file mode 100644 index 0000000..a9b5e35 --- /dev/null +++ b/integrationtest/command/testdata/editorconfig_override/before/a.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + labels: + app: nginx +spec: + replicas: 3 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 diff --git a/integrationtest/command/testdata/editorconfig_override/stdout/stderr.txt b/integrationtest/command/testdata/editorconfig_override/stdout/stderr.txt new file mode 100644 index 0000000..e69de29 diff --git a/integrationtest/command/testdata/editorconfig_override/stdout/stdout.txt b/integrationtest/command/testdata/editorconfig_override/stdout/stdout.txt new file mode 100644 index 0000000..e69de29 From f21710c9fd4983808b028d7f3736648d992e1f8d Mon Sep 17 00:00:00 2001 From: Oula Kuuva Date: Fri, 26 Sep 2025 09:57:57 +0300 Subject: [PATCH 4/4] refactor(editorconfig): use switch case --- formatters/basic/config.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/formatters/basic/config.go b/formatters/basic/config.go index dc1ca08..f85b821 100644 --- a/formatters/basic/config.go +++ b/formatters/basic/config.go @@ -61,11 +61,12 @@ func (c *Config) applyEditorConfig() { c.Indent = indent } - if ec.EndOfLine == "crlf" { + switch ec.EndOfLine { + case "crlf": c.LineEnding = yamlfmt.LineBreakStyleCRLF - } else if ec.EndOfLine == "lf" { + case "lf": c.LineEnding = yamlfmt.LineBreakStyleLF - } else if ec.EndOfLine == "cr" { + case "cr": // should we log this? }