@@ -8,7 +8,11 @@ import (
88 "github.com/elasticpath/epcc-cli/external/httpclient"
99 "github.com/elasticpath/epcc-cli/external/id"
1010 "github.com/elasticpath/epcc-cli/external/json"
11+ "github.com/yukithm/json2csv"
12+ "github.com/yukithm/json2csv/jsonpointer"
1113 "os"
14+ "sort"
15+ "strings"
1216 "sync"
1317
1418 "github.com/elasticpath/epcc-cli/external/resources"
@@ -25,12 +29,14 @@ type OutputFormat enumflag.Flag
2529
2630const (
2731 Jsonl OutputFormat = iota
32+ Json
2833 Csv
2934 EpccCli
3035)
3136
3237var OutputFormatIds = map [OutputFormat ][]string {
3338 Jsonl : {"jsonl" },
39+ Json : {"json" },
3440 Csv : {"csv" },
3541 EpccCli : {"epcc-cli" },
3642}
@@ -84,6 +90,30 @@ func NewGetAllCommand(parentCmd *cobra.Command) func() {
8490
8591}
8692
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+
87117func getAllInternal (ctx context.Context , outputFormat OutputFormat , outputFile string , args []string ) error {
88118 // Find Resource
89119 resource , ok := resources .GetResourceByName (args [0 ])
@@ -127,7 +157,7 @@ func getAllInternal(ctx context.Context, outputFormat OutputFormat, outputFile s
127157 if outputFile == "" {
128158 writer = os .Stdout
129159 } else {
130- file , err := os .Create (outputFile )
160+ file , err := os .OpenFile (outputFile , os . O_CREATE | os . O_WRONLY | os . O_APPEND , 0600 )
131161 if err != nil {
132162 panic (err )
133163 }
@@ -138,13 +168,15 @@ func getAllInternal(ctx context.Context, outputFormat OutputFormat, outputFile s
138168 outputWriter := func () {
139169 defer syncGroup .Done ()
140170
171+ csvLines := make ([]interface {}, 0 )
172+
173+ endMessages:
141174 for msgs := 0 ; ; msgs ++ {
142175 select {
143176 case result , ok := <- sendChannel :
144-
145177 if ! ok {
146178 log .Debugf ("Channel closed, we are done." )
147- return
179+ break endMessages
148180 }
149181 var obj interface {}
150182 err = gojson .Unmarshal (result .txt , & obj )
@@ -170,31 +202,140 @@ func getAllInternal(ctx context.Context, outputFormat OutputFormat, outputFile s
170202 },
171203 }
172204
173- line , err := gojson .Marshal (& wrappedObj )
205+ if outputFormat == Jsonl {
206+ err = writeJson (wrappedObj , writer )
174207
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 {}
179216
180- _ , err = writer .Write (line )
217+ sb .WriteString ("epcc create " )
218+ sb .WriteString (resource .SingularName )
181219
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 ("/" )
186226
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+ }
188234
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 )
193256
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+ }
194308 }
309+ }
310+ }
195311
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
196336 }
197337 }
338+
198339 }
199340
200341 go outputWriter ()
0 commit comments