Skip to content

Commit

Permalink
feat: read from config file for ignore migrate (#1223)
Browse files Browse the repository at this point in the history
* feat: read from config file for ignore migrate

* refactor: extract config file read to common method
  • Loading branch information
elsapet authored Aug 29, 2023
1 parent e51e018 commit e674387
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
12 changes: 4 additions & 8 deletions pkg/commands/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,11 @@ $ bearer ignore migrate`,
return fmt.Errorf("flag error: %s", err)
}

configFilePath := viper.GetString(flag.ConfigFileFlag.ConfigName)
fingerprintsToMigrate, err := getIgnoredFingerprintsFromConfig(configFilePath)
configFilePath, _, err := readConfig(args)
if err != nil {
return fmt.Errorf("error reading config: %s\nPerhaps you need to use --config-file to specify the config path?", err.Error())
}
fingerprintsToMigrate := getIgnoredFingerprintsFromConfig(configFilePath)

ignoredFingerprints, fileExists, err := ignore.GetIgnoredFingerprints(options.GeneralOptions.BearerIgnoreFile, nil)
if err != nil {
Expand Down Expand Up @@ -324,18 +324,14 @@ func writeIgnoreFile(ignoredFingerprints map[string]ignore.IgnoredFingerprint, b
return os.WriteFile(bearerIgnoreFilePath, data, 0644)
}

func getIgnoredFingerprintsFromConfig(configPath string) (ignoredFingerprintsFromConfig map[string]ignore.IgnoredFingerprint, err error) {
func getIgnoredFingerprintsFromConfig(configPath string) (ignoredFingerprintsFromConfig map[string]ignore.IgnoredFingerprint) {
ignoredFingerprintsFromConfig = make(map[string]ignore.IgnoredFingerprint)

if err := readConfig(configPath); err != nil {
return ignoredFingerprintsFromConfig, err
}

for _, fingerprint := range viper.GetStringSlice("report.exclude-fingerprint") {
ignoredFingerprintsFromConfig[fingerprint] = ignore.IgnoredFingerprint{
Comment: &migratedIgnoreComment,
}
}

return ignoredFingerprintsFromConfig, nil
return ignoredFingerprintsFromConfig
}
43 changes: 24 additions & 19 deletions pkg/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,14 @@ func NewScanCommand() *cobra.Command {
debugprofile.Start()
}

configPath := viper.GetString(flag.ConfigFileFlag.ConfigName)
var defaultConfigPath = ""
if len(args) > 0 {
defaultConfigPath = file.GetFullFilename(args[0], configPath)
}

var loadFileMessage string
if err := readConfig(configPath); err != nil {
if err := readConfig(defaultConfigPath); err != nil {
loadFileMessage = "Couldn't find any config file"
} else {
loadFileMessage = fmt.Sprintf("Loading default config file %s", defaultConfigPath)
}
} else {
loadFileMessage = fmt.Sprintf("Loading config file %s", configPath)
}
_, loadFileMessage, _ := readConfig(args)
log.Debug().Msgf(loadFileMessage)

options, err := ScanFlags.ToOptions(args)
if err != nil {
return fmt.Errorf("flag error: %s", err)
}

log.Debug().Msgf(loadFileMessage)

if len(args) == 0 {
return cmd.Help()
} else {
Expand All @@ -116,7 +100,28 @@ func NewScanCommand() *cobra.Command {
return cmd
}

func readConfig(configFile string) error {
func readConfig(args []string) (string, string, error) {
configPath := viper.GetString(flag.ConfigFileFlag.ConfigName)
var loadFileMessage string
if err := readConfigFromPath(configPath); err != nil {
// load from default
var configPath = ""
if len(args) > 0 {
configPath = file.GetFullFilename(args[0], configPath)
}
if err := readConfigFromPath(configPath); err != nil {
return configPath, "Couldn't find any config file", err
} else {
loadFileMessage = fmt.Sprintf("Loading default config file %s", configPath)
}
} else {
loadFileMessage = fmt.Sprintf("Loading config file %s", configPath)
}

return configPath, loadFileMessage, nil
}

func readConfigFromPath(configFile string) error {
viper.SetConfigType("yaml")
viper.SetConfigFile(configFile)
if err := viper.ReadInConfig(); err != nil {
Expand Down

0 comments on commit e674387

Please sign in to comment.