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
151153type fileVisitor struct {
152154 changesDetected bool
155+ sort bool
153156}
154157
155158func (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 {
0 commit comments