/
ext_output.go
81 lines (68 loc) · 1.6 KB
/
ext_output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package dwca
import (
"context"
"log/slog"
"path/filepath"
"github.com/gnames/dwca/pkg/ent/meta"
"golang.org/x/sync/errgroup"
)
func (a *arch) processExtensionsOutput() error {
for i := range a.meta.Extensions {
a.processExt(i)
}
return nil
}
func (a *arch) processExt(idx int) {
ext := a.meta.Extensions[idx]
extType := ext.RowType
extType = filepath.Base(extType)
slog.Info("Processing extension", "ext", extType)
var maxIdx int
for _, v := range ext.Fields {
if v.Idx > maxIdx {
maxIdx = v.Idx
}
}
a.updateOutputMetaExt(idx)
chIn := make(chan []string)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return a.saveExtOutput(ctx, idx, chIn)
})
_, err := a.ExtensionStream(ctx, idx, chIn)
if err != nil {
slog.Error(
"Error processing extension",
"file", ext.Files.Location,
"error", err,
)
}
}
func (a *arch) saveExtOutput(
ctx context.Context,
idx int,
chIn <-chan []string,
) error {
ext := a.outputMeta.Extensions[idx]
file := ext.Files.Location
fields := meta.Headers(idx, ext.Fields)
delim := ext.FieldsTerminatedBy
return a.dcFile.ExportCSVStream(ctx, file, fields, delim, chIn)
}
func (a *arch) updateOutputMetaExt(idx int) {
ext := a.outputMeta.Extensions[idx]
file := ext.Files.Location
e := filepath.Ext(file)
location := file[:len(file)-len(e)] + ".txt"
delim := ","
if a.cfg.OutputCSVType == "tsv" {
delim = `\t`
}
ext.Files.Location = location
ext.FieldsTerminatedBy = delim
ext.LinesTerminatedBy = `\n`
ext.FieldsEnclosedBy = `"`
ext.IgnoreHeaderLines = "1"
}