reader.go 885 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package detect
  2. import (
  3. "bufio"
  4. "io"
  5. "github.com/zricethezav/gitleaks/v8/report"
  6. )
  7. // DetectReader accepts an io.Reader and a buffer size for the reader in KB
  8. func (d *Detector) DetectReader(r io.Reader, bufSize int) ([]report.Finding, error) {
  9. reader := bufio.NewReader(r)
  10. buf := make([]byte, 0, 1000*bufSize)
  11. findings := []report.Finding{}
  12. for {
  13. n, err := reader.Read(buf[:cap(buf)])
  14. // "Callers should always process the n > 0 bytes returned before considering the error err."
  15. // https://pkg.go.dev/io#Reader
  16. if n > 0 {
  17. buf = buf[:n]
  18. fragment := Fragment{
  19. Raw: string(buf),
  20. }
  21. for _, finding := range d.Detect(fragment) {
  22. findings = append(findings, finding)
  23. if d.Verbose {
  24. printFinding(finding, d.NoColor)
  25. }
  26. }
  27. }
  28. if err != nil {
  29. if err != io.EOF {
  30. return findings, err
  31. }
  32. break
  33. }
  34. }
  35. return findings, nil
  36. }