Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: WithScanPath unexpected results #65

Merged
merged 2 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions internal/validate/path.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package validate

import (
"github.com/rs/zerolog/log"

"github.com/spf13/afero"
"os"
)

// Path checks if a file at the given path exists and returns it if so,
// PathExists checks if a file at the given path exists and returns it if so,
// otherwise returns a default path.
func Path(fs afero.Fs, path string) string {
ok, err := afero.Exists(fs, path)
if err != nil {
log.Fatal().Err(err).Send()
}

if ok {
return path
}

log.Fatal().Msg("no valid path provided")
return "."
func PathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
13 changes: 12 additions & 1 deletion pkg/hunter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,18 @@ func WithFS(fs afero.Fs) ConfigOption {

func WithScanPath(path string) ConfigOption {
return func(c *Config) {
c.ScanPath = validate.Path(c.Filesystem, c.ScanPath)
if validate.PathExists(path) {
c.ScanPath = path
return
}

currentDir, err := os.Getwd()
if err != nil {
log.Fatal().Err(err).Msg("failed to get current dir")
}

log.Error().Msgf("scan path %q not found, defaulting to %q", path, currentDir)
c.ScanPath = currentDir
}
}

Expand Down