feat: Iniital commit with pre-commit integrated

This commit is contained in:
2024-01-01 18:58:04 +00:00
parent abe94aa157
commit dee3b15156
6 changed files with 78 additions and 35 deletions

7
.devcontainer/Dockerfile Normal file
View File

@@ -0,0 +1,7 @@
FROM mcr.microsoft.com/devcontainers/go:1-1.21-bullseye
RUN sudo apt update && sudo apt install -y python3 python3-pip
RUN go install -v github.com/go-critic/go-critic/cmd/gocritic@latest \
&& go install github.com/fzipp/gocyclo/cmd/gocyclo@latest\
&& go install golang.org/x/tools/cmd/goimports@latest\
&& sudo pip3 install pre-commit

View File

@@ -3,23 +3,21 @@
{ {
"name": "Go", "name": "Go",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/go:1-1.21-bullseye", "build": {
// Path is relative to the devcontainer.json file.
"dockerfile": "Dockerfile"
},
"features": { "features": {
"ghcr.io/guiyomh/features/gotestsum:0": {} "ghcr.io/guiyomh/features/gotestsum:0": {}
} }
// Features to add to the dev container. More info: https://containers.dev/features. // Features to add to the dev container. More info: https://containers.dev/features.
// "features": {}, // "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally. // Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [], // "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created. // Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version", // "postCreateCommand": "go version",
// Configure tool-specific properties. // Configure tool-specific properties.
// "customizations": {}, // "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root" // "remoteUser": "root"
} }

1
.gitignore vendored
View File

@@ -198,3 +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

23
.pre-commit-config.yaml Normal file
View File

@@ -0,0 +1,23 @@
repos:
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.1
hooks:
- id: go-fmt
- id: go-vet
- id: go-lint
- id: go-imports
- id: go-cyclo
args: [-over=15]
- id: validate-toml
- id: no-go-testing
- id: golangci-lint
- id: go-critic
- id: go-unit-tests
- id: go-build
- id: go-mod-tidy
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace

View File

@@ -1,3 +1,15 @@
# gogrep # gogrep
Simple concurrent grep alternative Simple concurrent grep alternative
# Usage
TBD
# For developer
Simplest to use environment is already is VS Code Dev container.
Steps:
1. Open folder in Dev Container
2. Run `pre-commit install`. This tool will validate *new* commits according to checks defined in `.pre-commit-config.yaml`.

58
main.go
View File

@@ -10,15 +10,18 @@ import (
"regexp" "regexp"
) )
const CHANNEL_BUFFERING = 10 // ChannelBuffering is the size of file processing results queue
const ChannelBuffering = 10
// FileMatchData contains grep results
type FileMatchData struct { type FileMatchData struct {
Path string Path string
LineIndexes []int LineIndexes []int
Lines []string Lines []string
} }
func ProcessSingleFile(re_pattern regexp.Regexp, path string, out chan FileMatchData) error { // ProcessSingleFile processes file and send it Result to channel
func ProcessSingleFile(rePattern regexp.Regexp, path string, out chan FileMatchData) {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -28,45 +31,44 @@ func ProcessSingleFile(re_pattern regexp.Regexp, path string, out chan FileMatch
res := FileMatchData{Path: path, LineIndexes: make([]int, 0), Lines: make([]string, 0)} res := FileMatchData{Path: path, LineIndexes: make([]int, 0), Lines: make([]string, 0)}
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
// optionally, resize scanner's capacity for lines over 64K, see next example // optionally, resize scanner's capacity for lines over 64K, see next example
cur_line_ind := 1 curLineInd := 1
for scanner.Scan() { for scanner.Scan() {
line_text := scanner.Text() lineText := scanner.Text()
if re_pattern.FindString(line_text) != "" { if rePattern.FindString(lineText) != "" {
res.LineIndexes = append(res.LineIndexes, cur_line_ind) res.LineIndexes = append(res.LineIndexes, curLineInd)
res.Lines = append(res.Lines, line_text) res.Lines = append(res.Lines, lineText)
} }
cur_line_ind++ curLineInd++
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
log.Fatal(err) fmt.Printf("Error %v with file %v\n", err, path)
} }
out <- res out <- res
return nil
} }
func main() { func main() {
path_pattern := "git/hook" pathPattern := "git/hook"
re_path_pattern, err := regexp.Compile(path_pattern) rePathPattern, err := regexp.Compile(pathPattern)
if err != nil { if err != nil {
fmt.Printf("Unable to compile path pattern Regexp: %v\n", path_pattern) fmt.Printf("Unable to compile path pattern Regexp: %v\n", pathPattern)
return return
} }
text_pattern := "the commit" textPattern := "the commit"
re_text_pattern, err := regexp.Compile(text_pattern) reTextPattern, err := regexp.Compile(textPattern)
if err != nil { if err != nil {
fmt.Printf("Unable to compile text pattern Regexp: %v\n", text_pattern) fmt.Printf("Unable to compile text pattern Regexp: %v\n", textPattern)
return return
} }
processed_channel := make(chan FileMatchData, CHANNEL_BUFFERING) processedChannel := make(chan FileMatchData, ChannelBuffering)
total_files := 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 {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err) fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err return err
} }
if re_path_pattern.FindString(path) == "" { if rePathPattern.FindString(path) == "" {
return nil return nil
} }
// if info.IsDir() && info.Name() == subDirToSkip { // if info.IsDir() && info.Name() == subDirToSkip {
@@ -76,25 +78,25 @@ func main() {
if info.IsDir() { if info.IsDir() {
return nil return nil
} }
go ProcessSingleFile(*re_text_pattern, path, processed_channel) go ProcessSingleFile(*reTextPattern, path, processedChannel)
total_files++ totalFiles++
return nil return nil
}) })
for ; total_files > 0; total_files-- { for ; totalFiles > 0; totalFiles-- {
cur_processed := <-processed_channel curProcessed := <-processedChannel
if len(cur_processed.Lines) == 0 { if len(curProcessed.Lines) == 0 {
continue continue
} }
fmt.Printf("%s:\n", cur_processed.Path) fmt.Printf("%s:\n", curProcessed.Path)
for i := 0; i < len(cur_processed.Lines); i++ { for i := 0; i < len(curProcessed.Lines); i++ {
fmt.Printf("%d:%s\n", cur_processed.LineIndexes[i], cur_processed.Lines[i]) fmt.Printf("%d:%s\n", curProcessed.LineIndexes[i], curProcessed.Lines[i])
} }
fmt.Print("\n") fmt.Print("\n")
} }
if err != nil { if err != nil {
fmt.Printf("error walking the path %q: %v\n", path_pattern, err) fmt.Printf("error walking the path %q: %v\n", pathPattern, err)
return return
} }
} }