stdin.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package cmd
  2. import (
  3. "context"
  4. "os"
  5. "time"
  6. "github.com/spf13/cobra"
  7. "github.com/zricethezav/gitleaks/v8/logging"
  8. "github.com/zricethezav/gitleaks/v8/sources"
  9. )
  10. func init() {
  11. rootCmd.AddCommand(stdInCmd)
  12. }
  13. var stdInCmd = &cobra.Command{
  14. Use: "stdin",
  15. Short: "detect secrets from stdin",
  16. Run: runStdIn,
  17. }
  18. func runStdIn(cmd *cobra.Command, _ []string) {
  19. // start timer
  20. start := time.Now()
  21. // setup config (aka, the thing that defines rules)
  22. initConfig(".")
  23. initDiagnostics()
  24. cfg := Config(cmd)
  25. // create detector
  26. detector := Detector(cmd, cfg, "")
  27. // parse flag(s)
  28. exitCode := mustGetIntFlag(cmd, "exit-code")
  29. findings, err := detector.DetectSource(
  30. context.Background(),
  31. &sources.File{
  32. Content: os.Stdin,
  33. MaxArchiveDepth: detector.MaxArchiveDepth,
  34. },
  35. )
  36. if err != nil {
  37. // log fatal to exit, no need to continue since a report will not be
  38. // generated when scanning from a pipe...for now
  39. logging.Fatal().Err(err).Msg("failed scan input from stdin")
  40. }
  41. findingSummaryAndExit(detector, findings, exitCode, start, err)
  42. }