1
1
package pkg
2
2
3
3
import (
4
+ "bytes"
4
5
"fmt"
6
+ "os"
5
7
6
8
log "github.com/sirupsen/logrus"
7
9
"github.com/spf13/cobra"
@@ -26,6 +28,8 @@ func init() {
26
28
DeleteCmd .Flags ().StringVarP (& metadataType , "type" , "t" , "" , "metadata type" )
27
29
DeleteCmd .Flags ().StringVarP (& name , "name" , "n" , "" , "metadata item name" )
28
30
31
+ TidyCmd .Flags ().BoolP ("list" , "l" , false , "list files that need tidying" )
32
+
29
33
NewCmd .Flags ().StringVarP (& version , "version" , "v" , defaultVersion , "API version" )
30
34
31
35
AddCmd .MarkFlagRequired ("type" )
@@ -76,8 +80,18 @@ var TidyCmd = &cobra.Command{
76
80
DisableFlagsInUseLine : true ,
77
81
Args : cobra .MinimumNArgs (1 ),
78
82
Run : func (cmd * cobra.Command , args []string ) {
83
+ changes := false
79
84
for _ , file := range args {
80
- tidy (file )
85
+ list , _ := cmd .Flags ().GetBool ("list" )
86
+ if list {
87
+ needsTidying := checkIfChanged (file )
88
+ changes = needsTidying || changes
89
+ } else {
90
+ tidy (file )
91
+ }
92
+ }
93
+ if changes {
94
+ os .Exit (1 )
81
95
}
82
96
},
83
97
}
@@ -155,6 +169,26 @@ func listMembers(file string) {
155
169
}
156
170
}
157
171
172
+ func checkIfChanged (file string ) (changed bool ) {
173
+ o := & pkg.Package {}
174
+ contents , err := internal .ParseMetadataXmlIfPossible (o , file )
175
+ if err != nil {
176
+ log .Warn ("parse failure:" + err .Error ())
177
+ return
178
+ }
179
+ o .Tidy ()
180
+ newContents , err := internal .Marshal (o )
181
+ if err != nil {
182
+ log .Warn ("serializing failed: " + err .Error ())
183
+ return
184
+ }
185
+ if ! bytes .Equal (contents , newContents ) {
186
+ fmt .Println (file )
187
+ return true
188
+ }
189
+ return false
190
+ }
191
+
158
192
func tidy (file string ) {
159
193
p , err := pkg .Open (file )
160
194
if err != nil {
0 commit comments