diff --git a/internal/commands/hunt.go b/internal/commands/hunt.go index 2529acd..a35eef3 100644 --- a/internal/commands/hunt.go +++ b/internal/commands/hunt.go @@ -9,6 +9,7 @@ import ( "github.com/brittonhayes/pillager/pkg/format" "github.com/brittonhayes/pillager/pkg/hunter" + "github.com/brittonhayes/pillager/pkg/rules" "github.com/spf13/cobra" ) @@ -54,7 +55,14 @@ var huntCmd = &cobra.Command{ `, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + // Read gitleaks config from file + // or fallback to default + gitleaksConfig := rules.NewLoader( + rules.WithFile(rulesConfig), + ).Load() + h, err := hunter.New( + hunter.WithGitleaksConfig(gitleaksConfig), hunter.WithScanPath(args[0]), hunter.WithWorkers(workers), hunter.WithVerbose(verbose), diff --git a/internal/commands/root.go b/internal/commands/root.go index 6b4a5fc..2b477e5 100644 --- a/internal/commands/root.go +++ b/internal/commands/root.go @@ -43,6 +43,7 @@ func Execute() { } func init() { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.pillager.toml)") } @@ -70,7 +71,6 @@ func initConfig() { // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) log.Info().Msgf("Using config file: %q", viper.ConfigFileUsed()) } } diff --git a/pkg/hunter/config.go b/pkg/hunter/config.go index 0be035e..131c38a 100644 --- a/pkg/hunter/config.go +++ b/pkg/hunter/config.go @@ -47,7 +47,6 @@ func NewConfig(opts ...ConfigOption) *Config { defaultLogLevel = zerolog.ErrorLevel ) - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) zerolog.SetGlobalLevel(defaultLogLevel) config := &Config{ ScanPath: defaultScanPath, diff --git a/pkg/rules/rules.go b/pkg/rules/rules.go index cc75467..cae0c0c 100644 --- a/pkg/rules/rules.go +++ b/pkg/rules/rules.go @@ -5,6 +5,7 @@ import ( _ "embed" "github.com/BurntSushi/toml" + "github.com/brittonhayes/pillager/internal/validate" "github.com/rs/zerolog/log" "github.com/zricethezav/gitleaks/v8/config" @@ -64,11 +65,23 @@ func (l *Loader) Load() config.Config { return config } -// FromFile decodes a gitleaks config from a local file. -func FromFile(file string) LoaderOption { +// WithFile decodes a gitleaks config from a local file. +func WithFile(file string) LoaderOption { return func(l *Loader) { - if _, err := toml.DecodeFile(file, &l.loader); err != nil { - log.Fatal().Err(err).Msg(ErrReadConfig) + if file == "" { + if _, err := toml.Decode(RulesDefault, &l.loader); err != nil { + log.Fatal().Err(err).Msg(ErrReadConfig) + } + return + } + + if validate.PathExists(file) { + if _, err := toml.DecodeFile(file, &l.loader); err != nil { + log.Fatal().Err(err).Msg(ErrReadConfig) + } + return } + + log.Fatal().Msgf("invalid - rules file '%s' does not exist", file) } }