From 9463ffaa5e9efc94ee235972d1519eac2844fad3 Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Sat, 14 Sep 2024 04:01:27 -0500 Subject: [PATCH] fix flag access (#1506) --- cmd/detect.go | 17 +++++++---------- cmd/directory.go | 16 ++++++++++------ cmd/git.go | 16 +++++++++++----- cmd/protect.go | 11 +++++------ cmd/root.go | 6 +----- cmd/stdin.go | 2 +- 6 files changed, 35 insertions(+), 33 deletions(-) diff --git a/cmd/detect.go b/cmd/detect.go index 17b4e31f..32879817 100644 --- a/cmd/detect.go +++ b/cmd/detect.go @@ -46,11 +46,13 @@ var detectCmd = &cobra.Command{ } func runDetect(cmd *cobra.Command, args []string) { - initConfig() - var ( - findings []report.Finding - err error - ) + source, err := cmd.Flags().GetString("source") + if err != nil { + log.Fatal().Err(err).Msg("could not get source") + } + initConfig(source) + + var findings []report.Finding // setup config (aka, the thing that defines rules) cfg := Config(cmd) @@ -58,11 +60,6 @@ func runDetect(cmd *cobra.Command, args []string) { // 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 diff --git a/cmd/directory.go b/cmd/directory.go index d5176c06..8a052579 100644 --- a/cmd/directory.go +++ b/cmd/directory.go @@ -23,7 +23,15 @@ var directoryCmd = &cobra.Command{ } func runDirectory(cmd *cobra.Command, args []string) { - initConfig() + // grab source + source := "." + if len(args) == 1 { + source = args[0] + if source == "" { + source = "." + } + } + initConfig(source) var ( findings []report.Finding err error @@ -35,11 +43,7 @@ func runDirectory(cmd *cobra.Command, args []string) { // 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 diff --git a/cmd/git.go b/cmd/git.go index c1c020a2..8f033d9f 100644 --- a/cmd/git.go +++ b/cmd/git.go @@ -25,12 +25,22 @@ var gitCmd = &cobra.Command{ } func runGit(cmd *cobra.Command, args []string) { - initConfig() var ( findings []report.Finding err error ) + // grab source + source := "." + if len(args) == 1 { + source = args[0] + if source == "" { + source = "." + } + } + + initConfig(source) + // setup config (aka, the thing that defines rules) cfg := Config(cmd) @@ -38,10 +48,6 @@ func runGit(cmd *cobra.Command, args []string) { 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 diff --git a/cmd/protect.go b/cmd/protect.go index c04ab891..239d3ecb 100644 --- a/cmd/protect.go +++ b/cmd/protect.go @@ -25,18 +25,17 @@ var protectCmd = &cobra.Command{ } func runProtect(cmd *cobra.Command, args []string) { - initConfig() - var err error + source, err := cmd.Flags().GetString("source") + if err != nil { + log.Fatal().Err(err).Msg("could not get source") + } + initConfig(source) // setup config (aka, the thing that defines rules) cfg := Config(cmd) exitCode, _ := cmd.Flags().GetInt("exit-code") staged, _ := cmd.Flags().GetBool("staged") - source, err := cmd.Flags().GetString("source") - if err != nil { - log.Fatal().Err(err).Msg("") - } start := time.Now() detector := Detector(cmd, cfg, source) diff --git a/cmd/root.go b/cmd/root.go index 6a2b7667..f0b271a1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -86,7 +86,7 @@ func initLog() { } } -func initConfig() { +func initConfig(source string) { hideBanner, err := rootCmd.Flags().GetBool("no-banner") if err != nil { log.Fatal().Msg(err.Error()) @@ -106,10 +106,6 @@ func initConfig() { viper.SetConfigFile(envPath) log.Debug().Msgf("using gitleaks config from GITLEAKS_CONFIG env var: %s", envPath) } else { - source, err := rootCmd.Flags().GetString("source") - if err != nil { - log.Fatal().Msg(err.Error()) - } fileInfo, err := os.Stat(source) if err != nil { log.Fatal().Msg(err.Error()) diff --git a/cmd/stdin.go b/cmd/stdin.go index 217eae57..8357eea6 100644 --- a/cmd/stdin.go +++ b/cmd/stdin.go @@ -21,7 +21,7 @@ var stdInCmd = &cobra.Command{ } func runStdIn(cmd *cobra.Command, args []string) { - initConfig() + initConfig(".") var ( findings []report.Finding err error