feat: Add command line arguments
This commit is contained in:
4
go.mod
4
go.mod
@@ -1,3 +1,7 @@
|
|||||||
module gitea.likemath.ru/alex/gogrep
|
module gitea.likemath.ru/alex/gogrep
|
||||||
|
|
||||||
go 1.21
|
go 1.21
|
||||||
|
|
||||||
|
require github.com/alexflint/go-arg v1.4.3
|
||||||
|
|
||||||
|
require github.com/alexflint/go-scalar v1.1.0 // indirect
|
||||||
|
|||||||
16
go.sum
Normal file
16
go.sum
Normal file
@@ -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=
|
||||||
@@ -2,28 +2,38 @@ package cuiargs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
models "gitea.likemath.ru/alex/gogrep/internal/models"
|
models "gitea.likemath.ru/alex/gogrep/internal/models"
|
||||||
|
"github.com/alexflint/go-arg"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ProcessConsoleArguments fills internal grep config
|
// ProcessConsoleArguments fills internal grep config
|
||||||
// Returns error if arguments filling is correct, but arguments are incorrect
|
// Returns error if arguments filling is correct, but arguments are incorrect
|
||||||
// Panics and shows info if arguments are invalid
|
// Panics and shows info if arguments are invalid
|
||||||
func ProcessConsoleArguments() (*models.GrepConfigInternal, error) {
|
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{}
|
res := &models.GrepConfigInternal{}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
pathPattern := "git/hook"
|
res.PathPattern, err = regexp.Compile(args.Path)
|
||||||
res.PathPattern, err = regexp.Compile(pathPattern)
|
|
||||||
if err != nil {
|
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(args.Pattern)
|
||||||
res.TextPattern, err = regexp.Compile(textPattern)
|
|
||||||
if err != nil {
|
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
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user