feat: Add command line arguments

This commit is contained in:
2024-01-02 21:32:06 +00:00
parent 163d17eaf6
commit 88159da5b4
3 changed files with 36 additions and 6 deletions

View File

@@ -2,28 +2,38 @@ package cuiargs
import (
"fmt"
"log"
"regexp"
models "gitea.likemath.ru/alex/gogrep/internal/models"
"github.com/alexflint/go-arg"
)
// 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) {
var args struct {
Path string `arg:"positional" default:".*" help:"extra RegEx for paths to match"`
Pattern string `arg:"positional" help:"Regex for lines to match"`
}
arg.MustParse(&args)
if args.Pattern == "" {
log.Fatalf("No Pattern! Check `gogrep -h`")
}
res := &models.GrepConfigInternal{}
var err error
pathPattern := "git/hook"
res.PathPattern, err = regexp.Compile(pathPattern)
res.PathPattern, err = regexp.Compile(args.Path)
if err != nil {
return nil, fmt.Errorf("unable to compile path pattern Regexp: %v", pathPattern)
return nil, fmt.Errorf("unable to compile path pattern Regexp: %v", args.Path)
}
textPattern := "the commit"
res.TextPattern, err = regexp.Compile(textPattern)
res.TextPattern, err = regexp.Compile(args.Pattern)
if err != nil {
return nil, fmt.Errorf("unable to compile text pattern Regexp: %v", textPattern)
return nil, fmt.Errorf("unable to compile text pattern Regexp: %v", args.Pattern)
}
return res, nil
}