Skip to content

Commit

Permalink
fix: rules file read (#66)
Browse files Browse the repository at this point in the history
* fix: rules file read

* fix: gofumpt formatted

Co-authored-by: Britton Hayes <brittonhayes@users.noreply.github.com>
  • Loading branch information
brittonhayes and brittonhayes committed Mar 5, 2022
1 parent b7c6b93 commit e5dfe46
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
8 changes: 8 additions & 0 deletions internal/commands/hunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
Expand Down Expand Up @@ -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())
}
}
1 change: 0 additions & 1 deletion pkg/hunter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 17 additions & 4 deletions pkg/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit e5dfe46

Please sign in to comment.