refactor: Split code between packages

This commit is contained in:
2024-01-02 20:50:17 +00:00
parent dee3b15156
commit edc6cf37cd
5 changed files with 53 additions and 41 deletions

2
.gitignore vendored
View File

@@ -198,4 +198,4 @@ cython_debug/
# .nfs files are created when an open file is removed but is still being accessed # .nfs files are created when an open file is removed but is still being accessed
.nfs* .nfs*
gogrep /gogrep

View File

@@ -3,7 +3,7 @@ repos:
rev: v0.5.1 rev: v0.5.1
hooks: hooks:
- id: go-fmt - id: go-fmt
- id: go-vet #- id: go-vet
- id: go-lint - id: go-lint
- id: go-imports - id: go-imports
- id: go-cyclo - id: go-cyclo

View File

@@ -1,52 +1,18 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"io/fs" "io/fs"
"log"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
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 // ChannelBuffering is the size of file processing results queue
const ChannelBuffering = 10 const ChannelBuffering = 10
// FileMatchData contains grep results
type FileMatchData struct {
Path string
LineIndexes []int
Lines []string
}
// ProcessSingleFile processes file and send it Result to channel
func ProcessSingleFile(rePattern regexp.Regexp, path string, out chan FileMatchData) {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
res := FileMatchData{Path: path, LineIndexes: make([]int, 0), Lines: make([]string, 0)}
scanner := bufio.NewScanner(file)
// optionally, resize scanner's capacity for lines over 64K, see next example
curLineInd := 1
for scanner.Scan() {
lineText := scanner.Text()
if rePattern.FindString(lineText) != "" {
res.LineIndexes = append(res.LineIndexes, curLineInd)
res.Lines = append(res.Lines, lineText)
}
curLineInd++
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error %v with file %v\n", err, path)
}
out <- res
}
func main() { func main() {
pathPattern := "git/hook" pathPattern := "git/hook"
rePathPattern, err := regexp.Compile(pathPattern) rePathPattern, err := regexp.Compile(pathPattern)
@@ -61,7 +27,7 @@ func main() {
fmt.Printf("Unable to compile text pattern Regexp: %v\n", textPattern) fmt.Printf("Unable to compile text pattern Regexp: %v\n", textPattern)
return return
} }
processedChannel := make(chan FileMatchData, ChannelBuffering) processedChannel := make(chan models.FileMatchData, ChannelBuffering)
totalFiles := 0 totalFiles := 0
err = filepath.Walk(".", func(path string, info fs.FileInfo, err error) error { err = filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if err != nil { if err != nil {
@@ -78,7 +44,7 @@ func main() {
if info.IsDir() { if info.IsDir() {
return nil return nil
} }
go ProcessSingleFile(*reTextPattern, path, processedChannel) go processing.ProcessSingleFile(*reTextPattern, path, processedChannel)
totalFiles++ totalFiles++
return nil return nil

View File

@@ -0,0 +1,8 @@
package models
// FileMatchData contains grep results
type FileMatchData struct {
Path string
LineIndexes []int
Lines []string
}

View File

@@ -0,0 +1,38 @@
package processing
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
models "gitea.likemath.ru/alex/gogrep/internal/models"
)
// ProcessSingleFile processes file and send it Result to channel
func ProcessSingleFile(rePattern regexp.Regexp, path string, out chan models.FileMatchData) {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
res := models.FileMatchData{Path: path, LineIndexes: make([]int, 0), Lines: make([]string, 0)}
scanner := bufio.NewScanner(file)
// optionally, resize scanner's capacity for lines over 64K, see next example
curLineInd := 1
for scanner.Scan() {
lineText := scanner.Text()
if rePattern.FindString(lineText) != "" {
res.LineIndexes = append(res.LineIndexes, curLineInd)
res.Lines = append(res.Lines, lineText)
}
curLineInd++
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error %v with file %v\n", err, path)
}
out <- res
}