mirror of
https://github.com/packwiz/packwiz.git
synced 2025-11-19 01:24:32 +01:00
github: allow using a regular expression to match assets
safeguard against "undefined behavior" when a release has more than one asset Signed-off-by: unilock <unilock@fennet.rentals>
This commit is contained in:
@@ -3,6 +3,7 @@ package github
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
@@ -13,6 +14,7 @@ type ghUpdateData struct {
|
||||
Slug string `mapstructure:"slug"`
|
||||
Tag string `mapstructure:"tag"`
|
||||
Branch string `mapstructure:"branch"`
|
||||
Regex string `mapstructure:"regex"`
|
||||
}
|
||||
|
||||
type ghUpdater struct{}
|
||||
@@ -51,18 +53,29 @@ func (u ghUpdater) CheckUpdate(mods []*core.Mod, pack core.Pack) ([]core.UpdateC
|
||||
continue
|
||||
}
|
||||
|
||||
expr := regexp.MustCompile(data.Regex)
|
||||
|
||||
if len(newRelease.Assets) == 0 {
|
||||
results[i] = core.UpdateCheck{Error: errors.New("new release doesn't have any assets")}
|
||||
continue
|
||||
}
|
||||
|
||||
newFile := newRelease.Assets[0]
|
||||
var newFiles []Asset
|
||||
|
||||
for _, v := range newRelease.Assets {
|
||||
if strings.HasSuffix(v.Name, ".jar") {
|
||||
newFile = v
|
||||
if expr.MatchString(v.Name) {
|
||||
newFiles = append(newFiles, v)
|
||||
}
|
||||
}
|
||||
|
||||
if len(newFiles) > 1 {
|
||||
// TODO: also print file names
|
||||
results[i] = core.UpdateCheck{Error: errors.New("release has more than one asset matching regex")}
|
||||
continue
|
||||
}
|
||||
|
||||
newFile := newFiles[0]
|
||||
|
||||
results[i] = core.UpdateCheck{
|
||||
UpdateAvailable: true,
|
||||
UpdateString: mod.FileName + " -> " + newFile.Name,
|
||||
|
||||
Reference in New Issue
Block a user