-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* init backwards compatible cmd * update readme, todos * ... * moar comments, sprucing up root * readme change
- Loading branch information
1 parent
e93a7c0
commit 44ad62e
Showing
7 changed files
with
270 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/zricethezav/gitleaks/v8/report" | ||
"github.com/zricethezav/gitleaks/v8/sources" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(directoryCmd) | ||
directoryCmd.Flags().Bool("follow-symlinks", false, "scan files that are symlinks to other files") | ||
} | ||
|
||
var directoryCmd = &cobra.Command{ | ||
Use: "dir [flags] [path]", | ||
Aliases: []string{"file", "directory"}, | ||
Short: "scan directories or files for secrets", | ||
Run: runDirectory, | ||
} | ||
|
||
func runDirectory(cmd *cobra.Command, args []string) { | ||
initConfig() | ||
var ( | ||
findings []report.Finding | ||
err error | ||
) | ||
|
||
// setup config (aka, the thing that defines rules) | ||
cfg := Config(cmd) | ||
|
||
// start timer | ||
start := time.Now() | ||
|
||
// grab source | ||
source, err := cmd.Flags().GetString("source") | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("could not get source") | ||
} | ||
detector := Detector(cmd, cfg, source) | ||
|
||
// set exit code | ||
exitCode, err := cmd.Flags().GetInt("exit-code") | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("could not get exit code") | ||
} | ||
|
||
var paths <-chan sources.ScanTarget | ||
paths, err = sources.DirectoryTargets(source, detector.Sema, detector.FollowSymlinks) | ||
if err != nil { | ||
log.Fatal().Err(err) | ||
} | ||
|
||
findings, err = detector.DetectFiles(paths) | ||
if err != nil { | ||
// don't exit on error, just log it | ||
log.Error().Err(err).Msg("failed scan directory") | ||
} | ||
|
||
findingSummaryAndExit(findings, cmd, cfg, exitCode, start, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cmd | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/zricethezav/gitleaks/v8/report" | ||
"github.com/zricethezav/gitleaks/v8/sources" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(gitCmd) | ||
gitCmd.Flags().Bool("staged", false, "scan staged commits (good for pre-commit)") | ||
gitCmd.Flags().Bool("pre-commit", false, "scan using git diff") | ||
gitCmd.Flags().String("log-opts", "", "git log options") | ||
} | ||
|
||
var gitCmd = &cobra.Command{ | ||
Use: "git [flags] [repo]", | ||
Short: "scan git repositories for secrets", | ||
Args: cobra.MaximumNArgs(1), | ||
Run: runGit, | ||
} | ||
|
||
func runGit(cmd *cobra.Command, args []string) { | ||
initConfig() | ||
var ( | ||
findings []report.Finding | ||
err error | ||
) | ||
|
||
// setup config (aka, the thing that defines rules) | ||
cfg := Config(cmd) | ||
|
||
// start timer | ||
start := time.Now() | ||
|
||
// grab source | ||
source, err := cmd.Flags().GetString("source") | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("could not get source") | ||
} | ||
detector := Detector(cmd, cfg, source) | ||
|
||
// set exit code | ||
exitCode, err := cmd.Flags().GetInt("exit-code") | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("could not get exit code") | ||
} | ||
|
||
var ( | ||
gitCmd *sources.GitCmd | ||
logOpts string | ||
) | ||
logOpts, err = cmd.Flags().GetString("log-opts") | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("could not call GetString() for log-opts") | ||
} | ||
gitCmd, err = sources.NewGitLogCmd(source, logOpts) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("could not create Git cmd") | ||
} | ||
findings, err = detector.DetectGit(gitCmd) | ||
if err != nil { | ||
// don't exit on error, just log it | ||
log.Error().Err(err).Msg("failed to scan Git repository") | ||
} | ||
|
||
findingSummaryAndExit(findings, cmd, cfg, exitCode, start, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.