Skip to content

Commit ab65392

Browse files
lollipopmanstuart-warren
authored andcommitted
Disable sorting by default, but add a sort flag
Per the yaml spec maps are unordered and sequences are ordered. Sorting maps may break anchors & aliases. Sorting sequences may change their meaning for the consuming application. So disable sorting by default. Fixes: #15
1 parent dc0d738 commit ab65392

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

cmd/yamlfmt/main.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
write bool
2828
doDiff bool
2929
doFail bool
30+
doSort bool
3031

3132
errRequiresFmt = errors.New("RequiresFmt")
3233
)
@@ -48,8 +49,9 @@ func run(in io.Reader, out io.Writer, args []string) error {
4849
flags.BoolVar(&write, "w", false, "write result to (source) file instead of stdout")
4950
flags.BoolVar(&doDiff, "d", false, "display diffs instead of rewriting files")
5051
flags.BoolVar(&doFail, "f", false, "exit non zero if changes detected")
52+
flags.BoolVar(&doSort, "s", false, "sort maps & sequences, WARNING: This may break anchors & aliases")
5153
flags.Usage = func() {
52-
fmt.Fprintf(os.Stderr, "formats yaml files with 2 space indent, sorted dicts and non-indented lists\n")
54+
fmt.Fprintf(os.Stderr, "formats yaml files with 2 space indent and non-indented sequences\n")
5355
fmt.Fprintf(os.Stderr, "usage: yamlfmt [flags] [path ...]\n")
5456
flags.PrintDefaults()
5557
}
@@ -59,7 +61,7 @@ func run(in io.Reader, out io.Writer, args []string) error {
5961
if write {
6062
return fmt.Errorf("error: cannot use -w with standard input")
6163
}
62-
if err := processFile("<standard input>", in, out, true); err != nil {
64+
if err := processFile("<standard input>", in, out, true, doSort); err != nil {
6365
return err
6466
}
6567
}
@@ -70,9 +72,9 @@ func run(in io.Reader, out io.Writer, args []string) error {
7072
case err != nil:
7173
return err
7274
case dir.IsDir():
73-
return walkDir(path)
75+
return walkDir(path, doSort)
7476
default:
75-
if err := processFile(path, nil, os.Stdout, false); err != nil {
77+
if err := processFile(path, nil, os.Stdout, false, doSort); err != nil {
7678
return err
7779
}
7880
}
@@ -81,7 +83,7 @@ func run(in io.Reader, out io.Writer, args []string) error {
8183
}
8284

8385
// If in == nil, the source is the contents of the file with the given filename.
84-
func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error {
86+
func processFile(filename string, in io.Reader, out io.Writer, stdin bool, sort bool) error {
8587
var perm os.FileMode = 0644
8688
if in == nil {
8789
f, err := os.Open(filename)
@@ -102,7 +104,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
102104
return err
103105
}
104106

105-
res, err := yamlfmt.Format(bytes.NewBuffer(src))
107+
res, err := yamlfmt.Format(bytes.NewBuffer(src), sort)
106108
if err != nil {
107109
return err
108110
}
@@ -150,11 +152,12 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
150152

151153
type fileVisitor struct {
152154
changesDetected bool
155+
sort bool
153156
}
154157

155158
func (fv *fileVisitor) visitFile(path string, f os.FileInfo, err error) error {
156159
if err == nil && isYamlFile(f) {
157-
err = processFile(path, nil, os.Stdout, false)
160+
err = processFile(path, nil, os.Stdout, false, fv.sort)
158161
}
159162
// Don't complain if a file was deleted in the meantime (i.e.
160163
// the directory changed concurrently while running gofmt).
@@ -167,8 +170,8 @@ func (fv *fileVisitor) visitFile(path string, f os.FileInfo, err error) error {
167170
return nil
168171
}
169172

170-
func walkDir(path string) error {
171-
fv := fileVisitor{}
173+
func walkDir(path string, sort bool) error {
174+
fv := fileVisitor{sort: sort}
172175
filepath.Walk(path, fv.visitFile)
173176
var err error
174177
if fv.changesDetected {

format.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
const indent = 2
1212

1313
// Format reads in a yaml document and outputs the yaml in a standard format.
14-
// Dictionary keys are sorted lexicagraphically
14+
// If sort is true than dictionary keys are sorted lexicographically
1515
// Indents are set to 2
1616
// Lists are not indented
17-
func Format(r io.Reader) ([]byte, error) {
17+
func Format(r io.Reader, sort bool) ([]byte, error) {
1818
dec := yaml.NewDecoder(r)
1919
out := bytes.NewBuffer(nil)
2020
for {
@@ -30,7 +30,12 @@ func Format(r io.Reader) ([]byte, error) {
3030
return nil, fmt.Errorf("failed decoding: %s", err)
3131
}
3232
out.WriteString("---\n")
33-
if err := enc.Encode(sortYAML(&doc)); err != nil {
33+
if sort {
34+
err = enc.Encode(sortYAML(&doc))
35+
} else {
36+
err = enc.Encode(&doc)
37+
}
38+
if err != nil {
3439
return nil, fmt.Errorf("failed encoding: %s", err)
3540
}
3641
enc.Close()

format_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ k:
2727
c: i
2828
`
2929
exp := []byte(expected)
30-
out, err := yamlfmt.Format(bytes.NewReader([]byte(in)))
30+
out, err := yamlfmt.Format(bytes.NewReader([]byte(in)), true)
3131
if err != nil {
3232
t.Fatalf("Unexpected error: %s\n", err)
3333
}
@@ -50,7 +50,7 @@ bar:
5050
foo: baz # comment
5151
`
5252
exp := []byte(expected)
53-
out, err := yamlfmt.Format(bytes.NewReader([]byte(in)))
53+
out, err := yamlfmt.Format(bytes.NewReader([]byte(in)), true)
5454
if err != nil {
5555
t.Fatalf("Unexpected error: %s\n", err)
5656
}
@@ -97,7 +97,7 @@ k:
9797
c: i
9898
`
9999
exp := []byte(expected)
100-
out, err := yamlfmt.Format(bytes.NewReader([]byte(in)))
100+
out, err := yamlfmt.Format(bytes.NewReader([]byte(in)), true)
101101
if err != nil {
102102
t.Fatalf("Unexpected error: %s\n", err)
103103
}

0 commit comments

Comments
 (0)