Skip to content

Commit d1ad525

Browse files
Walzpierrelalanne
authored andcommitted
feat(output): Implement gzipped output and make gzip-jsonl the default format
1 parent 775032c commit d1ad525

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

cmd/src-fingerprint/main.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,16 @@ func getProvider(providerStr string, token string, providerOptions provider.Opti
4747
}
4848
}
4949

50-
func getExporter(exporterStr string, output io.Writer) (exporter.Exporter, error) {
50+
func getExporter(exporterStr string, output io.WriteCloser) (exporter.Exporter, error) {
5151
switch exporterStr {
5252
case "json":
5353
return exporter.NewJSONExporter(output), nil
54+
case "gzip-json":
55+
return exporter.NewGzipJSONExporter(output), nil
5456
case "jsonl":
5557
return exporter.NewJSONLExporter(output), nil
58+
case "gzip-jsonl":
59+
return exporter.NewGzipJSONLExporter(output), nil
5660
default:
5761
return nil, fmt.Errorf("invalid export format: %s", exporterStr)
5862
}
@@ -105,6 +109,11 @@ func main() {
105109
Value: "-",
106110
Usage: "set output path to `FILE`. stdout by default",
107111
},
112+
&cli.StringFlag{
113+
Name: "export-format",
114+
Value: "jsonl",
115+
Usage: "export format: 'jsonl'/'gzip-jsonl'/'json'/'gzip-json'. 'jsonl' by default",
116+
},
108117
&cli.StringFlag{
109118
Name: "clone-dir",
110119
Value: "-",
@@ -121,11 +130,6 @@ func main() {
121130
Required: true,
122131
Usage: "vcs provider. options: 'gitlab'/'github'/'bitbucket'/'repository'",
123132
},
124-
&cli.StringFlag{
125-
Name: "export-format",
126-
Value: "jsonl",
127-
Usage: "export format: 'jsonl'/'json'. jsonl by default",
128-
},
129133
&cli.StringFlag{
130134
Name: "token",
131135
Aliases: []string{"t"},
@@ -279,6 +283,5 @@ loop:
279283
}
280284

281285
log.Infoln("Done")
282-
283286
return nil
284287
}

exporter/output.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package exporter
22

33
import (
4+
"compress/gzip"
45
"encoding/json"
56
"io"
67
"srcfingerprint"
@@ -20,12 +21,23 @@ type Exporter interface {
2021
type JSONExporter struct {
2122
elements []*ExportGitFile
2223
encoder *json.Encoder
24+
writer io.WriteCloser
2325
}
2426

25-
func NewJSONExporter(output io.Writer) Exporter {
27+
func NewJSONExporter(output io.WriteCloser) Exporter {
2628
return &JSONExporter{
2729
elements: []*ExportGitFile{},
2830
encoder: json.NewEncoder(output),
31+
writer: output,
32+
}
33+
}
34+
35+
func NewGzipJSONExporter(output io.WriteCloser) Exporter {
36+
compressedWriter := gzip.NewWriter(output)
37+
return &JSONExporter{
38+
elements: []*ExportGitFile{},
39+
encoder: json.NewEncoder(compressedWriter),
40+
writer: compressedWriter,
2941
}
3042
}
3143

@@ -36,16 +48,32 @@ func (e *JSONExporter) AddElement(gitFile *ExportGitFile) error {
3648
}
3749

3850
func (e *JSONExporter) Close() error {
39-
return e.encoder.Encode(e.elements)
51+
if err1 := e.encoder.Encode(e.elements); err1 != nil {
52+
return err1
53+
}
54+
if err2 := e.writer.Close(); err2 != nil {
55+
return err2
56+
}
57+
return nil
4058
}
4159

4260
type JSONLExporter struct {
4361
encoder *json.Encoder
62+
writer io.WriteCloser
4463
}
4564

46-
func NewJSONLExporter(output io.Writer) Exporter {
65+
func NewJSONLExporter(output io.WriteCloser) Exporter {
4766
return &JSONLExporter{
4867
encoder: json.NewEncoder(output),
68+
writer: output,
69+
}
70+
}
71+
72+
func NewGzipJSONLExporter(output io.WriteCloser) Exporter {
73+
compressedWriter := gzip.NewWriter(output)
74+
return &JSONLExporter{
75+
encoder: json.NewEncoder(compressedWriter),
76+
writer: compressedWriter,
4977
}
5078
}
5179

@@ -54,5 +82,5 @@ func (e *JSONLExporter) AddElement(gitFile *ExportGitFile) error {
5482
}
5583

5684
func (e *JSONLExporter) Close() error {
57-
return nil
85+
return e.writer.Close()
5886
}

0 commit comments

Comments
 (0)