Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Empty file added b.yaml
Empty file.
41 changes: 40 additions & 1 deletion formatters/basic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -42,14 +45,50 @@ 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
}

switch ec.EndOfLine {
case "crlf":
c.LineEnding = yamlfmt.LineBreakStyleCRLF
case "lf":
c.LineEnding = yamlfmt.LineBreakStyleLF
case "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
}
16 changes: 13 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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 (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this need a go mod tidy; not sure why it added this extra require block.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's a good question. Another good one is why go mod tidy only touches go.sum...

github.com/editorconfig/editorconfig-core-go/v2 v2.6.3
golang.org/x/text v0.14.0 // indirect
)
13 changes: 10 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ 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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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.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=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -15,11 +17,16 @@ 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=
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=
24 changes: 24 additions & 0 deletions integrationtest/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.yaml]
indent_size = 3
trim_trailing_whitespace = true
21 changes: 21 additions & 0 deletions integrationtest/command/testdata/editorconfig_basic/after/a.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.yaml]
indent_size = 3
trim_trailing_whitespace = true
21 changes: 21 additions & 0 deletions integrationtest/command/testdata/editorconfig_basic/before/a.yaml
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.yaml]
indent_size = 3
trim_trailing_whitespace = true
21 changes: 21 additions & 0 deletions integrationtest/command/testdata/editorconfig_nested/after/a.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*.yaml]
indent_size = 4
trim_trailing_whitespace = false
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.yaml]
indent_size = 3
trim_trailing_whitespace = true
21 changes: 21 additions & 0 deletions integrationtest/command/testdata/editorconfig_nested/before/a.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*.yaml]
indent_size = 4
trim_trailing_whitespace = false
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.yaml]
indent_size = 3
trim_trailing_whitespace = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
formatter:
type: basic
indent: 2
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.