From 88159da5b413b1084815e141e792402fa5773b79 Mon Sep 17 00:00:00 2001 From: Aleksey Lobanov Date: Tue, 2 Jan 2024 21:32:06 +0000 Subject: [PATCH] feat: Add command line arguments --- go.mod | 4 ++++ go.sum | 16 ++++++++++++++++ internal/cui-args/args_reader.go | 22 ++++++++++++++++------ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index 1820f7f..9e50930 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ module gitea.likemath.ru/alex/gogrep go 1.21 + +require github.com/alexflint/go-arg v1.4.3 + +require github.com/alexflint/go-scalar v1.1.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9ef1991 --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/uo= +github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA= +github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM= +github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/cui-args/args_reader.go b/internal/cui-args/args_reader.go index f9e4d73..470124c 100644 --- a/internal/cui-args/args_reader.go +++ b/internal/cui-args/args_reader.go @@ -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 }