feat: add branch support for github mods

This commit is contained in:
Tricked 2022-06-15 16:38:07 +02:00 committed by unilock
parent cb9550f4a6
commit 5c07741447
3 changed files with 14 additions and 11 deletions

View File

@ -45,7 +45,7 @@ type ModReleases struct {
} `json:"author"` } `json:"author"`
NodeID string `json:"node_id"` NodeID string `json:"node_id"`
TagName string `json:"tag_name"` TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"` TargetCommitish string `json:"target_commitish"` // The branch of the release
Name string `json:"name"` Name string `json:"name"`
Draft bool `json:"draft"` Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"` Prerelease bool `json:"prerelease"`

View File

@ -8,7 +8,6 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"time" "time"
@ -18,9 +17,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var modSiteRegex = regexp.MustCompile("modrinth\\.com/mod/([^/]+)/?$")
var versionSiteRegex = regexp.MustCompile("modrinth\\.com/mod/([^/]+)/version/([^/]+)/?$")
// installCmd represents the install command // installCmd represents the install command
var installCmd = &cobra.Command{ var installCmd = &cobra.Command{
Use: "install [mod]", Use: "install [mod]",
@ -134,7 +130,7 @@ func fetchMod(slug string) (Mod, error) {
func installMod(mod Mod, pack core.Pack) error { func installMod(mod Mod, pack core.Pack) error {
fmt.Printf("Found mod %s: '%s'.\n", mod.Title, mod.Description) fmt.Printf("Found mod %s: '%s'.\n", mod.Title, mod.Description)
latestVersion, err := getLatestVersion(mod.Slug, pack) latestVersion, err := getLatestVersion(mod.Slug, pack, "")
if err != nil { if err != nil {
return fmt.Errorf("failed to get latest version: %v", err) return fmt.Errorf("failed to get latest version: %v", err)
} }
@ -145,7 +141,7 @@ func installMod(mod Mod, pack core.Pack) error {
return installVersion(mod, latestVersion, pack) return installVersion(mod, latestVersion, pack)
} }
func getLatestVersion(slug string, pack core.Pack) (ModReleases, error) { func getLatestVersion(slug string, pack core.Pack, branch string) (ModReleases, error) {
var modReleases []ModReleases var modReleases []ModReleases
var release ModReleases var release ModReleases
@ -171,6 +167,11 @@ func getLatestVersion(slug string, pack core.Pack) (ModReleases, error) {
if err != nil { if err != nil {
return release, err return release, err
} }
for _, r := range modReleases {
if r.TargetCommitish == branch {
return r, nil
}
}
return modReleases[0], nil return modReleases[0], nil
} }
@ -200,8 +201,9 @@ func installVersion(mod Mod, version ModReleases, pack core.Pack) error {
updateMap := make(map[string]map[string]interface{}) updateMap := make(map[string]map[string]interface{})
updateMap["github"], err = ghUpdateData{ updateMap["github"], err = ghUpdateData{
ModID: mod.ID, ModID: mod.Slug,
InstalledVersion: version.TagName, InstalledVersion: version.TagName,
Branch: version.TargetCommitish,
}.ToMap() }.ToMap()
if err != nil { if err != nil {
return err return err

View File

@ -12,6 +12,7 @@ import (
type ghUpdateData struct { type ghUpdateData struct {
ModID string `mapstructure:"mod-id"` // The slug of the repo but named modId for consistency reasons ModID string `mapstructure:"mod-id"` // The slug of the repo but named modId for consistency reasons
InstalledVersion string `mapstructure:"version"` InstalledVersion string `mapstructure:"version"`
Branch string `mapstructure:"branch"`
} }
type ghUpdater struct{} type ghUpdater struct{}
@ -31,7 +32,7 @@ func (u ghUpdater) CheckUpdate(mods []core.Mod, mcVersion string, pack core.Pack
results := make([]core.UpdateCheck, len(mods)) results := make([]core.UpdateCheck, len(mods))
for i, mod := range mods { for i, mod := range mods {
rawData, ok := mod.GetParsedUpdateData("modrinth") rawData, ok := mod.GetParsedUpdateData("github")
if !ok { if !ok {
results[i] = core.UpdateCheck{Error: errors.New("couldn't parse mod data")} results[i] = core.UpdateCheck{Error: errors.New("couldn't parse mod data")}
continue continue
@ -39,7 +40,7 @@ func (u ghUpdater) CheckUpdate(mods []core.Mod, mcVersion string, pack core.Pack
data := rawData.(ghUpdateData) data := rawData.(ghUpdateData)
newVersion, err := getLatestVersion(data.ModID, pack) newVersion, err := getLatestVersion(data.ModID, pack, data.Branch)
if err != nil { if err != nil {
results[i] = core.UpdateCheck{Error: fmt.Errorf("failed to get latest version: %v", err)} results[i] = core.UpdateCheck{Error: fmt.Errorf("failed to get latest version: %v", err)}
continue continue
@ -90,7 +91,7 @@ func (u ghUpdater) DoUpdate(mods []*core.Mod, cachedState []interface{}) error {
HashFormat: "sha256", HashFormat: "sha256",
Hash: hash, Hash: hash,
} }
mod.Update["modrinth"]["version"] = version.ID mod.Update["github"]["version"] = version.ID
} }
return nil return nil