@@ -5,15 +5,16 @@ import (
5
5
"encoding/json"
6
6
"errors"
7
7
"fmt"
8
+ "sort"
9
+ "strings"
10
+
8
11
"github.com/foomo/contentful"
12
+
9
13
"github.com/foomo/contentfulcommander/contentfulclient"
10
14
"github.com/foomo/contentfulcommander/model"
11
- "sort"
12
- "strings"
13
15
)
14
16
15
17
func Run (cma * contentful.Contentful , params []string ) error {
16
-
17
18
firstSpace , firstEnvironment := contentfulclient .GetSpaceAndEnvironment (params [0 ])
18
19
if firstSpace == "" {
19
20
return errors .New ("firstspace ID is empty" )
@@ -77,35 +78,32 @@ func getContentTypes(cma *contentful.Contentful, spaceID, environment string) (c
77
78
}
78
79
79
80
func diffContentTypes (firstSpaceName , secondSpaceName string , firstSpaceContentTypes , secondSpaceContentTypes []model.ContentType ) {
80
-
81
81
firstContentTypeMap ,
82
82
secondContentTypeMap ,
83
83
firstOnlyTypes ,
84
84
secondOnlyTypes ,
85
85
_ ,
86
- sortedTypes :=
87
- sliceElementsCompare (firstSpaceContentTypes , secondSpaceContentTypes ,
88
- func (contentType model.ContentType ) string {
89
- return contentType .Sys .ID
90
- })
86
+ sortedTypes := sliceElementsCompare (firstSpaceContentTypes , secondSpaceContentTypes ,
87
+ func (contentType model.ContentType ) string {
88
+ return contentType .Sys .ID
89
+ })
91
90
92
- const contentTypeHeader = "Content Type: '%s' %s\n "
93
91
for _ , contentTypeID := range sortedTypes {
94
92
if _ , ok := firstOnlyTypes [contentTypeID ]; ok {
95
- _ = printContentTypeHeader (contentTypeHeader , contentTypeID , false )
93
+ _ = printContentTypeHeader (contentTypeID , false )
96
94
fmt .Printf ("AAA ___ content type only available in %s\n " , firstSpaceName )
97
95
continue
98
96
}
99
97
if _ , ok := secondOnlyTypes [contentTypeID ]; ok {
100
- _ = printContentTypeHeader (contentTypeHeader , contentTypeID , false )
98
+ _ = printContentTypeHeader (contentTypeID , false )
101
99
fmt .Printf ("___ BBB content type only available in %s\n " , secondSpaceName )
102
100
continue
103
101
}
104
102
firstContentType := firstContentTypeMap [contentTypeID ]
105
103
secondContentType := secondContentTypeMap [contentTypeID ]
106
104
contentTypeHeaderAlreadyPrinted := false
107
105
if firstContentType .Name != secondContentType .Name {
108
- contentTypeHeaderAlreadyPrinted = printContentTypeHeader (contentTypeHeader , contentTypeID , contentTypeHeaderAlreadyPrinted )
106
+ contentTypeHeaderAlreadyPrinted = printContentTypeHeader (contentTypeID , contentTypeHeaderAlreadyPrinted )
109
107
fmt .Printf ("AAA BBB Name is different\n " )
110
108
fmt .Printf (" ^ ^----B: %s\n " , firstContentType .Name )
111
109
fmt .Printf (" ^--------A: %s\n " , secondContentType .Name )
@@ -123,27 +121,26 @@ func diffContentTypes(firstSpaceName, secondSpaceName string, firstSpaceContentT
123
121
firstOnlyFields ,
124
122
secondOnlyFields ,
125
123
_ ,
126
- sortedFields :=
127
- sliceElementsCompare (firstFields , secondFields ,
128
- func (field model.ContentTypeField ) string {
129
- return field .ID
130
- })
124
+ sortedFields := sliceElementsCompare (firstFields , secondFields ,
125
+ func (field model.ContentTypeField ) string {
126
+ return field .ID
127
+ })
131
128
for _ , fieldID := range sortedFields {
132
129
if _ , ok := firstOnlyFields [fieldID ]; ok {
133
- contentTypeHeaderAlreadyPrinted = printContentTypeHeader (contentTypeHeader , contentTypeID , contentTypeHeaderAlreadyPrinted )
130
+ contentTypeHeaderAlreadyPrinted = printContentTypeHeader (contentTypeID , contentTypeHeaderAlreadyPrinted )
134
131
fmt .Printf (" AAA ___ field '%s' only available in %s\n " , fieldID , firstSpaceName )
135
132
continue
136
133
}
137
134
if _ , ok := secondOnlyFields [fieldID ]; ok {
138
- contentTypeHeaderAlreadyPrinted = printContentTypeHeader (contentTypeHeader , contentTypeID , contentTypeHeaderAlreadyPrinted )
135
+ contentTypeHeaderAlreadyPrinted = printContentTypeHeader (contentTypeID , contentTypeHeaderAlreadyPrinted )
139
136
fmt .Printf (" ___ BBB field '%s' only available in %s\n " , fieldID , firstSpaceName )
140
137
continue
141
138
}
142
139
firstField := firstContentTypeFieldMap [fieldID ]
143
140
secondField := secondContentTypeFieldMap [fieldID ]
144
141
fieldHeaderAlreadyPrinted := false
145
142
printHeaders := func () {
146
- contentTypeHeaderAlreadyPrinted = printContentTypeHeader (contentTypeHeader , contentTypeID , contentTypeHeaderAlreadyPrinted )
143
+ contentTypeHeaderAlreadyPrinted = printContentTypeHeader (contentTypeID , contentTypeHeaderAlreadyPrinted )
147
144
fieldHeaderAlreadyPrinted = printFieldHeader (fieldID , fieldHeaderAlreadyPrinted )
148
145
}
149
146
if firstField .Name != secondField .Name {
@@ -174,14 +171,14 @@ func diffContentTypes(firstSpaceName, secondSpaceName string, firstSpaceContentT
174
171
printHeaders ()
175
172
printFieldValuesAB ("Required" , secondField .Required , firstField .Required )
176
173
}
177
- firstFieldValidations := getJsonString (firstField .Validations )
178
- secondFieldValidations := getJsonString (secondField .Validations )
174
+ firstFieldValidations := getJSONString (firstField .Validations )
175
+ secondFieldValidations := getJSONString (secondField .Validations )
179
176
if firstFieldValidations != secondFieldValidations {
180
177
printHeaders ()
181
178
printFieldValuesAB ("Validations" , firstFieldValidations , secondFieldValidations )
182
179
}
183
- firstFieldItems := getJsonString (firstField .Items )
184
- secondFieldItems := getJsonString (secondField .Items )
180
+ firstFieldItems := getJSONString (firstField .Items )
181
+ secondFieldItems := getJSONString (secondField .Items )
185
182
if firstFieldItems != secondFieldItems {
186
183
printHeaders ()
187
184
printFieldValuesAB ("Items" , firstFieldItems , secondFieldItems )
@@ -190,7 +187,7 @@ func diffContentTypes(firstSpaceName, secondSpaceName string, firstSpaceContentT
190
187
}
191
188
}
192
189
193
- func getJsonString (value any ) (stringValue string ) {
190
+ func getJSONString (value any ) (stringValue string ) {
194
191
byt , _ := json .Marshal (value )
195
192
stringValue = string (byt )
196
193
return
@@ -240,9 +237,9 @@ func sliceElementsCompare[A any](firstSlice, secondSlice []A, getID func(element
240
237
return firstObjectMap , secondObjectMap , firstOnly , secondOnly , common , sortedIDs
241
238
}
242
239
243
- func printContentTypeHeader (contentTypeHeader , contentTypeID string , contentTypeHeaderAlreadyPrinted bool ) bool {
240
+ func printContentTypeHeader (contentTypeID string , contentTypeHeaderAlreadyPrinted bool ) bool {
244
241
if ! contentTypeHeaderAlreadyPrinted {
245
- fmt .Printf (contentTypeHeader , contentTypeID , strings .Repeat ("-" , 80 - len (contentTypeID )))
242
+ fmt .Printf ("Content Type: '%s' %s \n " , contentTypeID , strings .Repeat ("-" , 80 - len (contentTypeID )))
246
243
}
247
244
return true
248
245
}
0 commit comments