github: use regexp2 for advanced regex

Signed-off-by: unilock <unilock@fennet.rentals>
This commit is contained in:
unilock 2024-04-16 07:04:13 -04:00
parent 979b606b1e
commit 7207d4c6a4
4 changed files with 11 additions and 5 deletions

View File

@ -9,6 +9,7 @@ import (
"path/filepath"
"regexp"
"github.com/dlclark/regexp2"
"github.com/packwiz/packwiz/core"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -120,7 +121,7 @@ func getLatestRelease(slug string, branch string) (Release, error) {
}
func installRelease(repo Repo, release Release, regex string, pack core.Pack) error {
expr := regexp.MustCompile(regex)
expr := regexp2.MustCompile(regex, 0)
if len(release.Assets) == 0 {
return errors.New("release doesn't have any assets attached")
@ -129,7 +130,8 @@ func installRelease(repo Repo, release Release, regex string, pack core.Pack) er
var files []Asset
for _, v := range release.Assets {
if expr.MatchString(v.Name) {
bl, _ := expr.MatchString(v.Name)
if bl {
files = append(files, v)
}
}

View File

@ -3,9 +3,9 @@ package github
import (
"errors"
"fmt"
"regexp"
"strings"
"github.com/dlclark/regexp2"
"github.com/mitchellh/mapstructure"
"github.com/packwiz/packwiz/core"
)
@ -53,7 +53,7 @@ func (u ghUpdater) CheckUpdate(mods []*core.Mod, pack core.Pack) ([]core.UpdateC
continue
}
expr := regexp.MustCompile(data.Regex)
expr := regexp2.MustCompile(data.Regex, 0)
if len(newRelease.Assets) == 0 {
results[i] = core.UpdateCheck{Error: errors.New("new release doesn't have any assets")}
@ -63,7 +63,8 @@ func (u ghUpdater) CheckUpdate(mods []*core.Mod, pack core.Pack) ([]core.UpdateC
var newFiles []Asset
for _, v := range newRelease.Assets {
if expr.MatchString(v.Name) {
bl, _ := expr.MatchString(v.Name)
if bl {
newFiles = append(newFiles, v)
}
}

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/aviddiviner/go-murmur v0.0.0-20150519214947-b9740d71e571
github.com/daviddengcn/go-colortext v1.0.0 // indirect
github.com/dlclark/regexp2 v1.11.0
github.com/fatih/camelcase v1.0.0
github.com/igorsobreira/titlecase v0.0.0-20140109233139-4156b5b858ac
github.com/kylelemons/godebug v1.1.0 // indirect

2
go.sum
View File

@ -66,6 +66,8 @@ 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/daviddengcn/go-colortext v1.0.0 h1:ANqDyC0ys6qCSvuEK7l3g5RaehL/Xck9EX8ATG8oKsE=
github.com/daviddengcn/go-colortext v1.0.0/go.mod h1:zDqEI5NVUop5QPpVJUxE9UO10hRnmkD5G4Pmri9+m4c=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=