feat: Move grep walk to processing package

This commit is contained in:
2024-01-02 21:10:05 +00:00
parent 8c39d6f40e
commit e526e378cc
2 changed files with 47 additions and 39 deletions

View File

@@ -2,57 +2,19 @@ package main
import (
"fmt"
"io/fs"
"log"
"path/filepath"
cui "gitea.likemath.ru/alex/gogrep/internal/cui-args"
models "gitea.likemath.ru/alex/gogrep/internal/models"
processing "gitea.likemath.ru/alex/gogrep/internal/processing"
)
// ChannelBuffering is the size of file processing results queue
const ChannelBuffering = 10
func main() {
config, err := cui.ProcessConsoleArguments()
if err != nil {
log.Fatalf("Invalid config: %v\n", err)
}
processedChannel := make(chan models.FileMatchData, ChannelBuffering)
totalFiles := 0
err = filepath.Walk(".", 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() && info.Name() == subDirToSkip {
// fmt.Printf("skipping a dir without errors: %+v \n", info.Name())
// return filepath.SkipDir
// }
if info.IsDir() {
return nil
}
go processing.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")
}
err = processing.DoGrepMain(config, ".")
if err != nil {
fmt.Printf("error walking the path %q: %v\n", config.PathPattern, err)
return

View File

@@ -0,0 +1,46 @@
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
}