package github import ( "errors" "fmt" "strings" "github.com/mitchellh/mapstructure" "github.com/packwiz/packwiz/core" ) type ghUpdateData struct { Slug string `mapstructure:"slug"` Tag string `mapstructure:"tag"` Branch string `mapstructure:"branch"` } type ghUpdater struct{} func (u ghUpdater) ParseUpdate(updateUnparsed map[string]interface{}) (interface{}, error) { var updateData ghUpdateData err := mapstructure.Decode(updateUnparsed, &updateData) return updateData, err } type cachedStateStore struct { ModID string Version Release } func (u ghUpdater) CheckUpdate(mods []*core.Mod, pack core.Pack) ([]core.UpdateCheck, error) { results := make([]core.UpdateCheck, len(mods)) for i, mod := range mods { rawData, ok := mod.GetParsedUpdateData("github") if !ok { results[i] = core.UpdateCheck{Error: errors.New("failed to parse update metadata")} continue } data := rawData.(ghUpdateData) newVersion, err := getLatestVersion(data.Slug, data.Branch) if err != nil { results[i] = core.UpdateCheck{Error: fmt.Errorf("failed to get latest version: %v", err)} continue } if newVersion.TagName == data.Tag { // The latest version from the site is the same as the installed one results[i] = core.UpdateCheck{UpdateAvailable: false} continue } if len(newVersion.Assets) == 0 { results[i] = core.UpdateCheck{Error: errors.New("new version doesn't have any assets")} continue } newFilename := newVersion.Assets[0].Name results[i] = core.UpdateCheck{ UpdateAvailable: true, UpdateString: mod.FileName + " -> " + newFilename, CachedState: cachedStateStore{data.Slug, newVersion}, } } return results, nil } func (u ghUpdater) DoUpdate(mods []*core.Mod, cachedState []interface{}) error { for i, mod := range mods { modState := cachedState[i].(cachedStateStore) var version = modState.Version var file = version.Assets[0] for _, v := range version.Assets { if strings.HasSuffix(v.Name, ".jar") { file = v } } hash, err := file.getSha256() if err != nil { return err } mod.FileName = file.Name mod.Download = core.ModDownload{ URL: file.BrowserDownloadURL, HashFormat: "sha256", Hash: hash, } mod.Update["github"]["tag"] = version.TagName } return nil }