From 7207d4c6a4195e21bfcd620d49e4768e9cb278d9 Mon Sep 17 00:00:00 2001 From: unilock Date: Tue, 16 Apr 2024 07:04:13 -0400 Subject: [PATCH] github: use regexp2 for advanced regex Signed-off-by: unilock --- github/install.go | 6 ++++-- github/updater.go | 7 ++++--- go.mod | 1 + go.sum | 2 ++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/github/install.go b/github/install.go index e47ca62..30e535c 100644 --- a/github/install.go +++ b/github/install.go @@ -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) } } diff --git a/github/updater.go b/github/updater.go index c970961..039388d 100644 --- a/github/updater.go +++ b/github/updater.go @@ -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) } } diff --git a/go.mod b/go.mod index 7692e9f..0a71260 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index aed0b7c..ada5dd2 100644 --- a/go.sum +++ b/go.sum @@ -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=