Skip to content

Commit 4385230

Browse files
authored
Merge pull request XmirrorSecurity#282 from luotianqi777/fix_csv
fix: csv report
2 parents 378b707 + 7b8797c commit 4385230

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

cmd/format/csv.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package format
33
import (
44
"fmt"
55
"io"
6+
"strings"
67

78
"github.com/xmirrorsecurity/opensca-cli/v3/cmd/detail"
89
)
@@ -18,8 +19,25 @@ func Csv(report Report, out string) {
1819
licenseTxt = n.Licenses[0].ShortName
1920
}
2021

22+
formatCsv := func(s string) string {
23+
if strings.Contains(s, `"`) {
24+
s = strings.ReplaceAll(s, `"`, `""`)
25+
}
26+
if strings.Contains(s, `,`) {
27+
s = fmt.Sprintf(`"%s"`, s)
28+
}
29+
return s
30+
}
31+
2132
if n.Name != "" {
22-
table = table + fmt.Sprintf("%s,%s,%s,%s,%s,%s\n", n.Name, n.Version, n.Vendor, licenseTxt, n.Language, n.Purl())
33+
table = table + fmt.Sprintf("%s,%s,%s,%s,%s,%s\n",
34+
formatCsv(n.Name),
35+
formatCsv(n.Version),
36+
formatCsv(n.Vendor),
37+
formatCsv(licenseTxt),
38+
formatCsv(n.Language),
39+
formatCsv(n.Purl()),
40+
)
2341
}
2442

2543
return true

0 commit comments

Comments
 (0)