this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd/cue: add a fmt --check flag to list badly formatted files

Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

fixes #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1189314
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

authored by

Noam Dolovich and committed by
Daniel Martí
4db44817 2887786d

+72 -2
+41 -1
cmd/cue/cmd/fmt.go
··· 15 15 package cmd 16 16 17 17 import ( 18 + "bytes" 19 + "cuelang.org/go/internal/source" 20 + "fmt" 18 21 "github.com/spf13/cobra" 22 + "os" 19 23 20 24 "cuelang.org/go/cue/ast" 21 25 "cuelang.org/go/cue/build" ··· 56 60 cfg.Format = opts 57 61 cfg.Force = true 58 62 63 + check := flagCheck.Bool(cmd) 64 + var badlyFormattedFiles []string 65 + 59 66 for _, inst := range builds { 60 67 if inst.Err != nil { 61 68 switch { ··· 67 74 } 68 75 } 69 76 for _, file := range inst.BuildFiles { 70 - files := []*ast.File{} 77 + // When using --check, we need to buffer the input and output bytes to compare them. 78 + var original []byte 79 + var formatted bytes.Buffer 80 + if check { 81 + if bs, ok := file.Source.([]byte); ok { 82 + original = bs 83 + } else { 84 + original, err = source.ReadAll(file.Filename, file.Source) 85 + exitOnErr(cmd, err, true) 86 + file.Source = original 87 + } 88 + cfg.Out = &formatted 89 + } 90 + 91 + var files []*ast.File 71 92 d := encoding.NewDecoder(file, &cfg) 72 93 for ; !d.Done(); d.Next() { 73 94 f := d.File() ··· 93 114 err := e.EncodeFile(f) 94 115 exitOnErr(cmd, err, false) 95 116 } 117 + 96 118 if err := e.Close(); err != nil { 97 119 exitOnErr(cmd, err, true) 120 + } 121 + 122 + if check && !bytes.Equal(formatted.Bytes(), original) { 123 + badlyFormattedFiles = append(badlyFormattedFiles, file.Filename) 98 124 } 99 125 } 100 126 } 127 + 128 + if check && len(badlyFormattedFiles) > 0 { 129 + stdout := cmd.OutOrStdout() 130 + for _, f := range badlyFormattedFiles { 131 + if f != "-" { 132 + fmt.Fprintln(stdout, f) 133 + } 134 + } 135 + os.Exit(1) 136 + } 137 + 101 138 return nil 102 139 }), 103 140 } 141 + 142 + cmd.Flags().Bool(string(flagCheck), false, "exits with non-zero status if any files are not formatted") 143 + 104 144 return cmd 105 145 }
+30
cmd/cue/cmd/testdata/script/fmt_check.txtar
··· 1 + # succeeds when file is formatted 2 + exec cue fmt --check formatted.cue 3 + 4 + stdin formatted.cue 5 + exec cue fmt --check - 6 + 7 + # fails and displays non formatted files 8 + ! exec cue fmt --check not_formatted.cue another_not_formatted.cue 9 + cmpenv stdout expected-output 10 + 11 + # files are not modified with --check 12 + # running twice returns the same file list 13 + ! exec cue fmt --check not_formatted.cue another_not_formatted.cue 14 + cmpenv stdout expected-output 15 + 16 + # stdin fails with no output 17 + stdin not_formatted.cue 18 + ! exec cue fmt --check - 19 + ! stdout . 20 + 21 + -- formatted.cue -- 22 + foo: "bar" 23 + -- not_formatted.cue -- 24 + foo: "bar" 25 + -- another_not_formatted.cue -- 26 + bar: "baz" 27 + x: 1 28 + -- expected-output -- 29 + $WORK${/}another_not_formatted.cue 30 + $WORK${/}not_formatted.cue
+1 -1
cue/ast/ast.go
··· 951 951 // ---------------------------------------------------------------------------- 952 952 // Files and packages 953 953 954 - // A File node represents a Go source file. 954 + // A File node represents a CUE source file. 955 955 // 956 956 // The Comments list contains all comments in the source file in order of 957 957 // appearance, including the comments that are pointed to from other nodes