diff --git a/cmd/gogrep/main.go b/cmd/gogrep/main.go index 8e75768..6a84ca6 100644 --- a/cmd/gogrep/main.go +++ b/cmd/gogrep/main.go @@ -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 diff --git a/internal/processing/walk.go b/internal/processing/walk.go new file mode 100644 index 0000000..5556a0e --- /dev/null +++ b/internal/processing/walk.go @@ -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 +}