stdin.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package cmd
  2. import (
  3. "os"
  4. "time"
  5. "github.com/spf13/cobra"
  6. "github.com/zricethezav/gitleaks/v8/logging"
  7. "github.com/zricethezav/gitleaks/v8/sources"
  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, _ []string) {
  18. // start timer
  19. start := time.Now()
  20. // setup config (aka, the thing that defines rules)
  21. initConfig(".")
  22. initDiagnostics()
  23. cfg := Config(cmd)
  24. // create detector
  25. detector := Detector(cmd, cfg, "")
  26. // parse flag(s)
  27. exitCode := mustGetIntFlag(cmd, "exit-code")
  28. findings, err := detector.DetectSource(
  29. cmd.Context(),
  30. &sources.File{
  31. Content: os.Stdin,
  32. MaxArchiveDepth: detector.MaxArchiveDepth,
  33. },
  34. )
  35. if err != nil {
  36. // log fatal to exit, no need to continue since a report will not be
  37. // generated when scanning from a pipe...for now
  38. logging.Fatal().Err(err).Msg("failed scan input from stdin")
  39. }
  40. findingSummaryAndExit(detector, findings, exitCode, start, err)
  41. }