stdin.go 912 B

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