csv.go 948 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package report
  2. import (
  3. "encoding/csv"
  4. "io"
  5. "strconv"
  6. "strings"
  7. )
  8. // writeCsv writes the list of findings to a writeCloser.
  9. func writeCsv(f []Finding, w io.WriteCloser) error {
  10. if len(f) == 0 {
  11. return nil
  12. }
  13. defer w.Close()
  14. cw := csv.NewWriter(w)
  15. err := cw.Write([]string{"RuleID",
  16. "Commit",
  17. "File",
  18. "SymlinkFile",
  19. "Secret",
  20. "Match",
  21. "StartLine",
  22. "EndLine",
  23. "StartColumn",
  24. "EndColumn",
  25. "Author",
  26. "Message",
  27. "Date",
  28. "Email",
  29. "Fingerprint",
  30. "Tags",
  31. })
  32. if err != nil {
  33. return err
  34. }
  35. for _, f := range f {
  36. err = cw.Write([]string{f.RuleID,
  37. f.Commit,
  38. f.File,
  39. f.SymlinkFile,
  40. f.Secret,
  41. f.Match,
  42. strconv.Itoa(f.StartLine),
  43. strconv.Itoa(f.EndLine),
  44. strconv.Itoa(f.StartColumn),
  45. strconv.Itoa(f.EndColumn),
  46. f.Author,
  47. f.Message,
  48. f.Date,
  49. f.Email,
  50. f.Fingerprint,
  51. strings.Join(f.Tags, " "),
  52. })
  53. if err != nil {
  54. return err
  55. }
  56. }
  57. cw.Flush()
  58. return cw.Error()
  59. }