refactor: Split code between packages
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -198,4 +198,4 @@ cython_debug/
|
||||
# .nfs files are created when an open file is removed but is still being accessed
|
||||
.nfs*
|
||||
|
||||
gogrep
|
||||
/gogrep
|
||||
|
||||
@@ -3,7 +3,7 @@ repos:
|
||||
rev: v0.5.1
|
||||
hooks:
|
||||
- id: go-fmt
|
||||
- id: go-vet
|
||||
#- id: go-vet
|
||||
- id: go-lint
|
||||
- id: go-imports
|
||||
- id: go-cyclo
|
||||
|
||||
@@ -1,52 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"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
|
||||
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() {
|
||||
pathPattern := "git/hook"
|
||||
rePathPattern, err := regexp.Compile(pathPattern)
|
||||
@@ -61,7 +27,7 @@ func main() {
|
||||
fmt.Printf("Unable to compile text pattern Regexp: %v\n", textPattern)
|
||||
return
|
||||
}
|
||||
processedChannel := make(chan FileMatchData, ChannelBuffering)
|
||||
processedChannel := make(chan models.FileMatchData, ChannelBuffering)
|
||||
totalFiles := 0
|
||||
err = filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
@@ -78,7 +44,7 @@ func main() {
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
go ProcessSingleFile(*reTextPattern, path, processedChannel)
|
||||
go processing.ProcessSingleFile(*reTextPattern, path, processedChannel)
|
||||
totalFiles++
|
||||
|
||||
return nil
|
||||
8
internal/models/file_match.go
Normal file
8
internal/models/file_match.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package models
|
||||
|
||||
// FileMatchData contains grep results
|
||||
type FileMatchData struct {
|
||||
Path string
|
||||
LineIndexes []int
|
||||
Lines []string
|
||||
}
|
||||
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