47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package processing
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"path/filepath"
|
|
|
|
models "gitea.likemath.ru/alex/gogrep/internal/models"
|
|
)
|
|
|
|
// ChannelBuffering is the size of file processing results queue
|
|
const ChannelBuffering = 10
|
|
|
|
// DoGrepMain performs full grep accorgind to config and root
|
|
func DoGrepMain(config *models.GrepConfigInternal, root string) error {
|
|
processedChannel := make(chan models.FileMatchData, ChannelBuffering)
|
|
totalFiles := 0
|
|
err := filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
|
|
if err != nil {
|
|
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
|
|
return err
|
|
}
|
|
if config.PathPattern.FindString(path) == "" {
|
|
return nil
|
|
}
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
go ProcessSingleFile(config.TextPattern, path, processedChannel)
|
|
totalFiles++
|
|
|
|
return nil
|
|
})
|
|
for ; totalFiles > 0; totalFiles-- {
|
|
curProcessed := <-processedChannel
|
|
if len(curProcessed.Lines) == 0 {
|
|
continue
|
|
}
|
|
fmt.Printf("%s:\n", curProcessed.Path)
|
|
for i := 0; i < len(curProcessed.Lines); i++ {
|
|
fmt.Printf("%d:%s\n", curProcessed.LineIndexes[i], curProcessed.Lines[i])
|
|
}
|
|
fmt.Print("\n")
|
|
}
|
|
return err
|
|
}
|