feat: Move grep walk to processing package
This commit is contained in:
@@ -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
|
||||
|
||||
46
internal/processing/walk.go
Normal file
46
internal/processing/walk.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user