Skip to content

Commit c126d75

Browse files
committed
fix(cli): stores import csv strip all BOMs from payload.
feat(cli): `stores import csv` preserve header order in `_results.csv` Signed-off-by: spbsoluble <1661003+spbsoluble@users.noreply.github.com>
1 parent 8a1c308 commit c126d75

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

cmd/helpers.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/http"
2424
"os"
2525
"path/filepath"
26+
"slices"
2627
"strconv"
2728
"time"
2829

@@ -132,7 +133,7 @@ func csvToMap(filename string) ([]map[string]string, error) {
132133

133134
// Populate the map with data from the row
134135
for i, column := range header {
135-
rowMap[column] = row[i]
136+
rowMap[column] = stripAllBOMs(row[i])
136137
}
137138

138139
// Append the map to the data slice
@@ -282,27 +283,32 @@ func logGlobals() {
282283

283284
}
284285

285-
func mapToCSV(data []map[string]string, filename string) error {
286-
file, err := os.Create(filename)
287-
if err != nil {
288-
return err
286+
func mapToCSV(data []map[string]string, filename string, inputHeader []string) error {
287+
file, fErr := os.Create(filename)
288+
if fErr != nil {
289+
return fErr
289290
}
290291
defer file.Close()
291292

292293
writer := csv.NewWriter(file)
293294
defer writer.Flush()
294295

295296
// Write the header using keys from the first map
296-
var header []string
297-
if len(data) > 0 {
297+
var header = inputHeader
298+
if len(header) <= 0 && len(data) > 0 {
298299
for key := range data[0] {
299-
header = append(header, key)
300-
}
301-
if err := writer.Write(header); err != nil {
302-
return err
300+
header = append(header, stripAllBOMs(key))
303301
}
304302
}
305303

304+
errorColFound := slices.Contains(header, "Errors")
305+
if !errorColFound {
306+
header = append(header, "Errors")
307+
}
308+
if hErr := writer.Write(header); hErr != nil {
309+
return hErr
310+
}
311+
306312
// Write map data to CSV
307313
for _, row := range data {
308314
var record []string

cmd/storesBulkOperations.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,14 @@ If you do not wish to include credentials in your CSV file they can be provided
311311
}
312312

313313
log.Info().Msgf("Processing CSV rows from file '%s'", filePath)
314+
var inputHeader []string
314315
for idx, row := range inFile {
315316
log.Debug().Msgf("Processing row '%d'", idx)
316317
originalMap = append(originalMap, row)
317318

318319
if idx == 0 {
319320
// skip header row
321+
inputHeader = row
320322
log.Debug().Msgf("Skipping header row")
321323
continue
322324
}
@@ -411,7 +413,7 @@ If you do not wish to include credentials in your CSV file they can be provided
411413
log.Info().Msgf("Writing results to file '%s'", outPath)
412414

413415
//writeCsvFile(outPath, originalMap)
414-
mapToCSV(inputMap, outPath)
416+
mapToCSV(inputMap, outPath, inputHeader)
415417
log.Info().Int("totalRows", totalRows).
416418
Int("totalSuccesses", totalSuccess).
417419
Int("errorCount", errorCount).

0 commit comments

Comments
 (0)