Skip to content

Commit e331e06

Browse files
committed
Merge branch 'tidy-list-package'
2 parents a8856dc + 400c745 commit e331e06

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

cmd/pkg/edit.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package pkg
22

33
import (
4+
"bytes"
45
"fmt"
6+
"os"
57

68
log "github.com/sirupsen/logrus"
79
"github.com/spf13/cobra"
@@ -26,6 +28,8 @@ func init() {
2628
DeleteCmd.Flags().StringVarP(&metadataType, "type", "t", "", "metadata type")
2729
DeleteCmd.Flags().StringVarP(&name, "name", "n", "", "metadata item name")
2830

31+
TidyCmd.Flags().BoolP("list", "l", false, "list files that need tidying")
32+
2933
NewCmd.Flags().StringVarP(&version, "version", "v", defaultVersion, "API version")
3034

3135
AddCmd.MarkFlagRequired("type")
@@ -76,8 +80,18 @@ var TidyCmd = &cobra.Command{
7680
DisableFlagsInUseLine: true,
7781
Args: cobra.MinimumNArgs(1),
7882
Run: func(cmd *cobra.Command, args []string) {
83+
changes := false
7984
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)
8195
}
8296
},
8397
}
@@ -155,6 +169,26 @@ func listMembers(file string) {
155169
}
156170
}
157171

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+
158192
func tidy(file string) {
159193
p, err := pkg.Open(file)
160194
if err != nil {

0 commit comments

Comments
 (0)