diff --git a/.gitignore b/.gitignore index 6f137b7..3ffccfd 100644 --- a/.gitignore +++ b/.gitignore @@ -198,4 +198,4 @@ cython_debug/ # .nfs files are created when an open file is removed but is still being accessed .nfs* -gogrep +/gogrep diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a8f133c..0a29de4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/main.go b/cmd/gogrep/main.go similarity index 58% rename from main.go rename to cmd/gogrep/main.go index ab7eb37..3a0eec1 100644 --- a/main.go +++ b/cmd/gogrep/main.go @@ -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 diff --git a/internal/models/file_match.go b/internal/models/file_match.go new file mode 100644 index 0000000..9126311 --- /dev/null +++ b/internal/models/file_match.go @@ -0,0 +1,8 @@ +package models + +// FileMatchData contains grep results +type FileMatchData struct { + Path string + LineIndexes []int + Lines []string +} diff --git a/internal/processing/processing.go b/internal/processing/processing.go new file mode 100644 index 0000000..edbd22a --- /dev/null +++ b/internal/processing/processing.go @@ -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 +}