refactor: flatten nested config and tool fallback logic
This commit is contained in:
1 parent
79a4db5362
commit
63cff4fe53
3 files changed
+107
-83
No files matched your search
@@ -376,60 +376,68 @@ func grepFallback(ctx context.Context, p GrepParams, searchPath, cwd string, tar
|
||||
}
|
||||
}
|
||||
results = append(results, res...)
|
||||
} else {
|
||||
err = filepath.WalkDir(searchPath, func(walkPath string, d os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return nil //nolint:nilerr // intentionally skip inaccessible entries
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
if d.IsDir() {
|
||||
// Skip hidden directories.
|
||||
if d.Name() != "" && d.Name()[0] == '.' && walkPath != searchPath {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Skip hidden files.
|
||||
if d.Name() != "" && d.Name()[0] == '.' {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Include filter.
|
||||
if p.Include != "" {
|
||||
if matched, _ := filepath.Match(p.Include, d.Name()); !matched {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
res, err := grepFile(ctx, walkPath, matcher, p.ContextBefore, p.ContextAfter)
|
||||
if err != nil {
|
||||
return nil //nolint:nilerr // intentionally skip files we can't read
|
||||
}
|
||||
for i := range res {
|
||||
res[i].relPath, _ = filepath.Rel(cwd, walkPath)
|
||||
res[i].relPath = filepath.ToSlash(res[i].relPath)
|
||||
if res[i].relPath == "" || res[i].relPath == "." {
|
||||
res[i].relPath = filepath.ToSlash(filepath.Base(walkPath))
|
||||
}
|
||||
}
|
||||
results = append(results, res...)
|
||||
return nil
|
||||
})
|
||||
if err != nil && !errors.Is(err, ctx.Err()) {
|
||||
return "", err
|
||||
}
|
||||
} else if err := grepDirFallback(ctx, searchPath, cwd, p, matcher, &results); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return formatGrepResults(results), nil
|
||||
}
|
||||
|
||||
// grepDirFallback walks searchPath and appends every readable, non-hidden file
|
||||
// matching the include filter to results. It skips hidden directories and
|
||||
// unreadable files without failing the whole search.
|
||||
func grepDirFallback(ctx context.Context, searchPath, cwd string, p GrepParams, matcher func(string) bool, results *[]grepResult) error {
|
||||
err := filepath.WalkDir(searchPath, func(walkPath string, d os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return nil //nolint:nilerr // intentionally skip inaccessible entries
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
if d.IsDir() {
|
||||
// Skip hidden directories.
|
||||
if d.Name() != "" && d.Name()[0] == '.' && walkPath != searchPath {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Skip hidden files.
|
||||
if d.Name() != "" && d.Name()[0] == '.' {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Include filter.
|
||||
if p.Include != "" {
|
||||
if matched, _ := filepath.Match(p.Include, d.Name()); !matched {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
res, err := grepFile(ctx, walkPath, matcher, p.ContextBefore, p.ContextAfter)
|
||||
if err != nil {
|
||||
return nil //nolint:nilerr // intentionally skip files we can't read
|
||||
}
|
||||
for i := range res {
|
||||
res[i].relPath, _ = filepath.Rel(cwd, walkPath)
|
||||
res[i].relPath = filepath.ToSlash(res[i].relPath)
|
||||
if res[i].relPath == "" || res[i].relPath == "." {
|
||||
res[i].relPath = filepath.ToSlash(filepath.Base(walkPath))
|
||||
}
|
||||
}
|
||||
*results = append(*results, res...)
|
||||
return nil
|
||||
})
|
||||
if err != nil && !errors.Is(err, ctx.Err()) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildMatcher returns a line matcher based on grep parameters.
|
||||
func buildMatcher(p GrepParams) (func(string) bool, error) {
|
||||
if !p.Literal {
|
||||
|
||||
@@ -97,22 +97,8 @@ func suggestPaths(cwd, missing string) string {
|
||||
// shape of the real misses (internal/theme/theme.go, where the directory
|
||||
// exists but holds colors.go), and it avoids the walk entirely.
|
||||
if dir := filepath.Dir(missing); dir != "" {
|
||||
if entries, err := os.ReadDir(dir); err == nil {
|
||||
names := make([]string, 0, maxPathSuggestions)
|
||||
for _, e := range entries {
|
||||
if len(names) >= maxPathSuggestions {
|
||||
names = append(names, "...")
|
||||
break
|
||||
}
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
if len(names) > 0 {
|
||||
rel, relErr := filepath.Rel(cwd, dir)
|
||||
if relErr != nil {
|
||||
rel = dir
|
||||
}
|
||||
return fmt.Sprintf("%s exists and contains: %s", rel, strings.Join(names, ", "))
|
||||
}
|
||||
if suggestion := dirContentsSuggestion(cwd, dir); suggestion != "" {
|
||||
return suggestion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,6 +136,31 @@ func suggestPaths(cwd, missing string) string {
|
||||
return "did you mean: " + strings.Join(found, ", ")
|
||||
}
|
||||
|
||||
// dirContentsSuggestion lists the entries in an existing directory as a
|
||||
// "did you mean" hint, or returns "" when the directory is unreadable or empty.
|
||||
func dirContentsSuggestion(cwd, dir string) string {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
names := make([]string, 0, maxPathSuggestions)
|
||||
for _, e := range entries {
|
||||
if len(names) >= maxPathSuggestions {
|
||||
names = append(names, "...")
|
||||
break
|
||||
}
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
if len(names) == 0 {
|
||||
return ""
|
||||
}
|
||||
rel, relErr := filepath.Rel(cwd, dir)
|
||||
if relErr != nil {
|
||||
rel = dir
|
||||
}
|
||||
return fmt.Sprintf("%s exists and contains: %s", rel, strings.Join(names, ", "))
|
||||
}
|
||||
|
||||
// skipSuggestionDir keeps the suggestion walk out of directories that are large
|
||||
// and never the answer.
|
||||
func skipSuggestionDir(name string) bool {
|
||||
|
||||
Reference in new issue
Block a user