@@ -8,7 +8,11 @@ import (
8
8
"github.com/elasticpath/epcc-cli/external/httpclient"
9
9
"github.com/elasticpath/epcc-cli/external/id"
10
10
"github.com/elasticpath/epcc-cli/external/json"
11
+ "github.com/yukithm/json2csv"
12
+ "github.com/yukithm/json2csv/jsonpointer"
11
13
"os"
14
+ "sort"
15
+ "strings"
12
16
"sync"
13
17
14
18
"github.com/elasticpath/epcc-cli/external/resources"
@@ -25,12 +29,14 @@ type OutputFormat enumflag.Flag
25
29
26
30
const (
27
31
Jsonl OutputFormat = iota
32
+ Json
28
33
Csv
29
34
EpccCli
30
35
)
31
36
32
37
var OutputFormatIds = map [OutputFormat ][]string {
33
38
Jsonl : {"jsonl" },
39
+ Json : {"json" },
34
40
Csv : {"csv" },
35
41
EpccCli : {"epcc-cli" },
36
42
}
@@ -84,6 +90,30 @@ func NewGetAllCommand(parentCmd *cobra.Command) func() {
84
90
85
91
}
86
92
93
+ func writeJson (obj interface {}, writer io.Writer ) error {
94
+ line , err := gojson .Marshal (& obj )
95
+
96
+ if err != nil {
97
+ return fmt .Errorf ("could not create JSON for %s, error: %v" , line , err )
98
+
99
+ }
100
+
101
+ _ , err = writer .Write (line )
102
+
103
+ if err != nil {
104
+ return fmt .Errorf ("could not save line %s, error: %v" , line , err )
105
+
106
+ }
107
+
108
+ _ , err = writer .Write ([]byte {10 })
109
+
110
+ if err != nil {
111
+ return fmt .Errorf ("Could not save line %s, error: %v" , line , err )
112
+ }
113
+
114
+ return nil
115
+ }
116
+
87
117
func getAllInternal (ctx context.Context , outputFormat OutputFormat , outputFile string , args []string ) error {
88
118
// Find Resource
89
119
resource , ok := resources .GetResourceByName (args [0 ])
@@ -127,7 +157,7 @@ func getAllInternal(ctx context.Context, outputFormat OutputFormat, outputFile s
127
157
if outputFile == "" {
128
158
writer = os .Stdout
129
159
} else {
130
- file , err := os .Create (outputFile )
160
+ file , err := os .OpenFile (outputFile , os . O_CREATE | os . O_WRONLY | os . O_APPEND , 0600 )
131
161
if err != nil {
132
162
panic (err )
133
163
}
@@ -138,13 +168,15 @@ func getAllInternal(ctx context.Context, outputFormat OutputFormat, outputFile s
138
168
outputWriter := func () {
139
169
defer syncGroup .Done ()
140
170
171
+ csvLines := make ([]interface {}, 0 )
172
+
173
+ endMessages:
141
174
for msgs := 0 ; ; msgs ++ {
142
175
select {
143
176
case result , ok := <- sendChannel :
144
-
145
177
if ! ok {
146
178
log .Debugf ("Channel closed, we are done." )
147
- return
179
+ break endMessages
148
180
}
149
181
var obj interface {}
150
182
err = gojson .Unmarshal (result .txt , & obj )
@@ -170,31 +202,140 @@ func getAllInternal(ctx context.Context, outputFormat OutputFormat, outputFile s
170
202
},
171
203
}
172
204
173
- line , err := gojson .Marshal (& wrappedObj )
205
+ if outputFormat == Jsonl {
206
+ err = writeJson (wrappedObj , writer )
174
207
175
- if err != nil {
176
- log .Errorf ("Could not create JSON for %s, error: %v" , line , err )
177
- continue
178
- }
208
+ if err != nil {
209
+ log .Errorf ("Error writing JSON line: %v" , err )
210
+ continue
211
+ }
212
+ } else if outputFormat == Json || outputFormat == Csv {
213
+ csvLines = append (csvLines , wrappedObj )
214
+ } else if outputFormat == EpccCli {
215
+ sb := & strings.Builder {}
179
216
180
- _ , err = writer .Write (line )
217
+ sb .WriteString ("epcc create " )
218
+ sb .WriteString (resource .SingularName )
181
219
182
- if err != nil {
183
- log .Errorf ("Could not save line %s, error: %v" , line , err )
184
- continue
185
- }
220
+ sb .WriteString (" " )
221
+ sb .WriteString ("--save-as-alias" )
222
+ sb .WriteString (" " )
223
+ sb .WriteString ("exported_source_id=" )
224
+ sb .WriteString (resource .SingularName )
225
+ sb .WriteString ("/" )
186
226
187
- _ , err = writer .Write ([]byte {10 })
227
+ if mp , ok := newObj .(map [string ]interface {}); ok {
228
+ sb .WriteString (fmt .Sprintf ("%s" , mp ["id" ]))
229
+ } else {
230
+ log .Errorf ("Error casting newObj to map[string]interface{}" )
231
+ sb .WriteString ("\n " )
232
+ continue
233
+ }
188
234
189
- if err != nil {
190
- log .Errorf ("Could not save line %s, error: %v" , line , err )
191
- continue
192
- }
235
+ for _ , resId := range result .id {
236
+ sb .WriteString (" " )
237
+ sb .WriteString ("exported_source_id=" )
238
+ sb .WriteString (resources .MustGetResourceByName (resId .EpccCliType ).SingularName )
239
+ sb .WriteString ("/" )
240
+ sb .WriteString (resId .Id )
241
+
242
+ }
243
+
244
+ kvs , err := json2csv .JSON2CSV (newObj )
245
+ if err != nil {
246
+ log .Errorf ("Error generating Key/Value pairs: %v" , err )
247
+ sb .WriteString ("\n " )
248
+ continue
249
+ }
250
+
251
+ for _ , kv := range kvs {
252
+
253
+ keys := kv .Keys ()
254
+
255
+ sort .Strings (keys )
193
256
257
+ for _ , k := range keys {
258
+ v := kv [k ]
259
+
260
+ jp , err := jsonpointer .New (k )
261
+
262
+ if err != nil {
263
+ log .Errorf ("Couldn't generate JSON Pointer for %s: %v" , k , err )
264
+
265
+ continue
266
+ }
267
+
268
+ jsonPointerKey := jp .DotNotation (true )
269
+
270
+ if strings .HasPrefix (jsonPointerKey , "meta." ) {
271
+ continue
272
+ }
273
+
274
+ if strings .HasPrefix (jsonPointerKey , "links." ) {
275
+ continue
276
+ }
277
+
278
+ if jsonPointerKey == "id" {
279
+ continue
280
+ }
281
+
282
+ if jsonPointerKey == "type" {
283
+ continue
284
+ }
285
+
286
+ sb .WriteString (" " )
287
+ sb .WriteString (jsonPointerKey )
288
+ sb .WriteString (" " )
289
+
290
+ if s , ok := v .(string ); ok {
291
+ sb .WriteString (`'` )
292
+ sb .WriteString (strings .ReplaceAll (s , `'` , `\'` ))
293
+ sb .WriteString (`'` )
294
+ } else {
295
+ sb .WriteString (fmt .Sprintf ("%v" , v ))
296
+ }
297
+
298
+ }
299
+ }
300
+
301
+ sb .WriteString ("\n " )
302
+ _ , err = writer .Write ([]byte (sb .String ()))
303
+
304
+ if err != nil {
305
+ log .Errorf ("Error writing command: %v" , err )
306
+ }
307
+ }
194
308
}
309
+ }
310
+ }
195
311
312
+ if outputFormat == Json {
313
+ err = writeJson (csvLines , writer )
314
+
315
+ if err != nil {
316
+ log .Errorf ("Error writing JSON line: %v" , err )
317
+ }
318
+ } else if outputFormat == Csv {
319
+
320
+ // Create writer that saves to string
321
+ results , err := json2csv .JSON2CSV (csvLines )
322
+
323
+ if err != nil {
324
+ log .Errorf ("Error converting to CSV: %v" , err )
325
+ return
326
+ }
327
+
328
+ csvWriter := json2csv .NewCSVWriter (writer )
329
+
330
+ csvWriter .HeaderStyle = json2csv .DotBracketStyle
331
+ csvWriter .Transpose = false
332
+
333
+ if err := csvWriter .WriteCSV (results ); err != nil {
334
+ log .Errorf ("Error writing CSV: %v" , err )
335
+ return
196
336
}
197
337
}
338
+
198
339
}
199
340
200
341
go outputWriter ()
0 commit comments