Skip to content

Commit 82a9944

Browse files
committed
Merge branch 'tidy-multiple-types'
2 parents dd0077c + 63c3a6d commit 82a9944

File tree

23 files changed

+1051
-531
lines changed

23 files changed

+1051
-531
lines changed

application/application.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
"github.com/ForceCLI/force-md/internal"
88
)
99

10+
const NAME = "CustomApplication"
11+
12+
func init() {
13+
internal.TypeRegistry.Register(NAME, func(path string) (internal.RegisterableMetadata, error) { return Open(path) })
14+
}
15+
1016
type ProfileActionOverride struct {
1117
ActionName string `xml:"actionName"`
1218
Content *string `xml:"content"`
@@ -112,6 +118,10 @@ func (c *CustomApplication) SetMetadata(m internal.MetadataInfo) {
112118
c.MetadataInfo = m
113119
}
114120

121+
func (c *CustomApplication) Type() internal.MetadataType {
122+
return NAME
123+
}
124+
115125
func Open(path string) (*CustomApplication, error) {
116126
p := &CustomApplication{}
117127
return p, internal.ParseMetadataXml(p, path)

cmd/objects/tidy.go

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,37 @@ Tidy object metadata.
3838
* picklist fields missing from Record Types
3939
`,
4040
Args: cobra.MinimumNArgs(1),
41-
Run: func(cmd *cobra.Command, args []string) {
41+
RunE: func(cmd *cobra.Command, args []string) error {
4242
changes := false
43+
list, _ := cmd.Flags().GetBool("list")
44+
fixMissing, _ := cmd.Flags().GetBool("fix-missing")
45+
fix := fixes{
46+
recordTypePicklistOptions: fixMissing,
47+
}
4348
for _, file := range args {
44-
list, _ := cmd.Flags().GetBool("list")
45-
fixMissing, _ := cmd.Flags().GetBool("fix-missing")
46-
fix := fixes{
47-
recordTypePicklistOptions: fixMissing,
49+
_, err := internal.Metadata.Open(file)
50+
if err != nil {
51+
return fmt.Errorf("invalid file %s: %w", file, err)
4852
}
53+
}
54+
55+
items := internal.Metadata.Items(objects.NAME)
56+
if len(items) == 0 {
57+
log.Warn("No objects to tidy")
58+
}
59+
for _, item := range items {
60+
o := item.(*objects.CustomObject)
4961
if list {
50-
needsTidying := checkIfChanged(file, fix)
62+
needsTidying := checkIfChanged(o, fix)
5163
changes = needsTidying || changes
5264
} else {
53-
tidy(file, fix)
65+
tidy(o, fix)
5466
}
5567
}
5668
if changes {
5769
os.Exit(1)
5870
}
71+
return nil
5972
},
6073
}
6174

@@ -153,13 +166,8 @@ func addMissingRecordTypePicklistFields(o *objects.CustomObject) {
153166
}
154167
}
155168

156-
func checkIfChanged(file string, fix fixes) (changed bool) {
157-
o := &objects.CustomObject{}
158-
contents, err := internal.ParseMetadataXmlIfPossible(o, file)
159-
if err != nil {
160-
log.Warn("parse failure:" + err.Error())
161-
return
162-
}
169+
func checkIfChanged(o *objects.CustomObject, fix fixes) (changed bool) {
170+
contents := o.Contents()
163171
if fix.recordTypePicklistOptions {
164172
addMissingRecordTypePicklistFields(o)
165173
}
@@ -173,25 +181,20 @@ func checkIfChanged(file string, fix fixes) (changed bool) {
173181
return
174182
}
175183
if !bytes.Equal(contents, newContents) {
176-
fmt.Println(file)
184+
fmt.Println(o.Path())
177185
return true
178186
}
179187
return false
180188
}
181189

182-
func tidy(file string, fix fixes) {
183-
p, err := objects.Open(file)
184-
if err != nil {
185-
log.Warn("parsing object failed: " + err.Error())
186-
return
187-
}
190+
func tidy(o *objects.CustomObject, fix fixes) {
188191
if warn {
189-
checkUnassignedPicklistOptions(p)
192+
checkUnassignedPicklistOptions(o)
190193
}
191194
if fix.recordTypePicklistOptions {
192-
addMissingRecordTypePicklistFields(p)
195+
addMissingRecordTypePicklistFields(o)
193196
}
194-
if err := general.Tidy(p, file); err != nil {
197+
if err := general.Tidy(o, o.Path()); err != nil {
195198
log.Warn("tidying failed: " + err.Error())
196199
}
197200
}

cmd/tidy.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os"
7+
8+
_ "github.com/ForceCLI/force-md/flow"
9+
10+
"github.com/ForceCLI/force-md/general"
11+
"github.com/ForceCLI/force-md/internal"
12+
13+
log "github.com/sirupsen/logrus"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
func init() {
18+
tidyCmd.Flags().BoolP("list", "l", false, "list files that need tidying")
19+
20+
RootCmd.AddCommand(tidyCmd)
21+
}
22+
23+
var tidyCmd = &cobra.Command{
24+
Use: "tidy",
25+
Short: "Tidy Metadata",
26+
Example: `
27+
$ force-md tidy sfdx/main/default/objects/*/{fields,validationRules}/* sfdx/main/default/flows/*
28+
29+
$ force-md tidy src/objects/*
30+
`,
31+
Args: cobra.MinimumNArgs(1),
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
for _, file := range args {
34+
_, err := internal.Metadata.Open(file)
35+
if err != nil {
36+
return fmt.Errorf("invalid file %s: %w", file, err)
37+
}
38+
}
39+
changes := false
40+
list, _ := cmd.Flags().GetBool("list")
41+
for _, t := range internal.Metadata.Types() {
42+
for _, m := range internal.Metadata.Items(t) {
43+
file := m.GetMetadataInfo().Path()
44+
o, ok := m.(general.Tidyable)
45+
if !ok {
46+
log.Warnf("file %s of type %T is not tidyable", file, m)
47+
continue
48+
}
49+
if list {
50+
orig := m.GetMetadataInfo().Contents()
51+
needsTidying := checkIfChanged(o, orig)
52+
if needsTidying {
53+
fmt.Println(file)
54+
}
55+
changes = needsTidying || changes
56+
} else {
57+
if err := general.Tidy(o, file); err != nil {
58+
log.Warnf("tidying failed: %s", err.Error())
59+
}
60+
}
61+
}
62+
}
63+
if changes {
64+
os.Exit(1)
65+
}
66+
return nil
67+
},
68+
}
69+
70+
func checkIfChanged(o general.Tidyable, orig []byte) (changed bool) {
71+
o.Tidy()
72+
newContents, err := internal.Marshal(o)
73+
if err != nil {
74+
log.Warn("serializing failed: " + err.Error())
75+
return false
76+
}
77+
if !bytes.Equal(orig, newContents) {
78+
return true
79+
}
80+
return false
81+
}

docs/force-md.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ force-md
3434
* [force-md reporttype](force-md_reporttype.md) - Manage Report Types
3535
* [force-md sharingrules](force-md_sharingrules.md) - Manage Sharing Rules
3636
* [force-md standardvalueset](force-md_standardvalueset.md) - Manage Standard Value Sets
37+
* [force-md tidy](force-md_tidy.md) - Tidy Metadata
3738
* [force-md version](force-md_version.md) - Display current version
3839
* [force-md workflows](force-md_workflows.md) - Manage Workflow
3940

docs/force-md_tidy.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## force-md tidy
2+
3+
Tidy Metadata
4+
5+
```
6+
force-md tidy [flags]
7+
```
8+
9+
### Examples
10+
11+
```
12+
13+
$ force-md tidy sfdx/main/default/objects/*/{fields,validationRules}/* sfdx/main/default/flows/*
14+
15+
$ force-md tidy src/objects/*
16+
17+
```
18+
19+
### Options
20+
21+
```
22+
-h, --help help for tidy
23+
-l, --list list files that need tidying
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
--convert-xml-entities convert numeric xml entities to character entities (default true)
30+
--silent show errors only
31+
```
32+
33+
### SEE ALSO
34+
35+
* [force-md](force-md.md) - force-md manipulate Salesforce metadata
36+

0 commit comments

Comments
 (0)