stdin.go 1.0 KB

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