refactor: Split code between packages
This commit is contained in:
38
internal/processing/processing.go
Normal file
38
internal/processing/processing.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user