feat: Package for command-line arguments
This commit is contained in:
@@ -3,9 +3,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"log"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
|
||||||
|
|
||||||
|
cui "gitea.likemath.ru/alex/gogrep/internal/cui-args"
|
||||||
models "gitea.likemath.ru/alex/gogrep/internal/models"
|
models "gitea.likemath.ru/alex/gogrep/internal/models"
|
||||||
processing "gitea.likemath.ru/alex/gogrep/internal/processing"
|
processing "gitea.likemath.ru/alex/gogrep/internal/processing"
|
||||||
)
|
)
|
||||||
@@ -14,18 +15,9 @@ import (
|
|||||||
const ChannelBuffering = 10
|
const ChannelBuffering = 10
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pathPattern := "git/hook"
|
config, err := cui.ProcessConsoleArguments()
|
||||||
rePathPattern, err := regexp.Compile(pathPattern)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Unable to compile path pattern Regexp: %v\n", pathPattern)
|
log.Fatalf("Invalid config: %v\n", err)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
textPattern := "the commit"
|
|
||||||
reTextPattern, err := regexp.Compile(textPattern)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Unable to compile text pattern Regexp: %v\n", textPattern)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
processedChannel := make(chan models.FileMatchData, ChannelBuffering)
|
processedChannel := make(chan models.FileMatchData, ChannelBuffering)
|
||||||
totalFiles := 0
|
totalFiles := 0
|
||||||
@@ -34,7 +26,7 @@ func main() {
|
|||||||
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 rePathPattern.FindString(path) == "" {
|
if config.PathPattern.FindString(path) == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// if info.IsDir() && info.Name() == subDirToSkip {
|
// if info.IsDir() && info.Name() == subDirToSkip {
|
||||||
@@ -44,7 +36,7 @@ func main() {
|
|||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
go processing.ProcessSingleFile(*reTextPattern, path, processedChannel)
|
go processing.ProcessSingleFile(config.TextPattern, path, processedChannel)
|
||||||
totalFiles++
|
totalFiles++
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -62,7 +54,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error walking the path %q: %v\n", pathPattern, err)
|
fmt.Printf("error walking the path %q: %v\n", config.PathPattern, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
29
internal/cui-args/args_reader.go
Normal file
29
internal/cui-args/args_reader.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package cuiargs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
models "gitea.likemath.ru/alex/gogrep/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ProcessConsoleArguments fills internal grep config
|
||||||
|
// Returns error if arguments filling is correct, but arguments are incorrect
|
||||||
|
// Panics and shows info if arguments are invalid
|
||||||
|
func ProcessConsoleArguments() (*models.GrepConfigInternal, error) {
|
||||||
|
res := &models.GrepConfigInternal{}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
pathPattern := "git/hook"
|
||||||
|
res.PathPattern, err = regexp.Compile(pathPattern)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to compile path pattern Regexp: %v", pathPattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
textPattern := "the commit"
|
||||||
|
res.TextPattern, err = regexp.Compile(textPattern)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to compile text pattern Regexp: %v", textPattern)
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
9
internal/models/config.go
Normal file
9
internal/models/config.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "regexp"
|
||||||
|
|
||||||
|
// GrepConfigInternal defines valid grep configuration after parameters parsing
|
||||||
|
type GrepConfigInternal struct {
|
||||||
|
PathPattern *regexp.Regexp
|
||||||
|
TextPattern *regexp.Regexp
|
||||||
|
}
|
||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ProcessSingleFile processes file and send it Result to channel
|
// ProcessSingleFile processes file and send it Result to channel
|
||||||
func ProcessSingleFile(rePattern regexp.Regexp, path string, out chan models.FileMatchData) {
|
func ProcessSingleFile(rePattern *regexp.Regexp, path string, out chan models.FileMatchData) {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user