Add migrate command (#207)

* Add base and loader command

* Support updating to latest

* Support explicit fabric versions

* Support explicit Forge version

* Support quilt

* Add support for updating Minecraft

* Add support for Forge recommended

* Fix Forge version sorting

* Various fixes

* Add documentation to the use

* More suggestions
This commit is contained in:
Matt Artist
2023-04-12 21:23:37 -04:00
committed by GitHub
parent 06f9204cd4
commit 539be71d11
9 changed files with 354 additions and 57 deletions

52
cmdshared/mcversion.go Normal file
View File

@@ -0,0 +1,52 @@
package cmdshared
import (
"encoding/json"
"fmt"
"github.com/packwiz/packwiz/core"
"os"
"sort"
"time"
)
type McVersionManifest struct {
Latest struct {
Release string `json:"release"`
Snapshot string `json:"snapshot"`
} `json:"latest"`
Versions []struct {
ID string `json:"id"`
Type string `json:"type"`
URL string `json:"url"`
Time time.Time `json:"time"`
ReleaseTime time.Time `json:"releaseTime"`
} `json:"versions"`
}
func (m McVersionManifest) CheckValid(version string) {
for _, v := range m.Versions {
if v.ID == version {
return
}
}
fmt.Println("Given version is not a valid Minecraft version!")
os.Exit(1)
}
func GetValidMCVersions() (McVersionManifest, error) {
res, err := core.GetWithUA("https://launchermeta.mojang.com/mc/game/version_manifest.json", "application/json")
if err != nil {
return McVersionManifest{}, err
}
dec := json.NewDecoder(res.Body)
out := McVersionManifest{}
err = dec.Decode(&out)
if err != nil {
return McVersionManifest{}, err
}
// Sort by newest to oldest
sort.Slice(out.Versions, func(i, j int) bool {
return out.Versions[i].ReleaseTime.Before(out.Versions[j].ReleaseTime)
})
return out, nil
}

16
cmdshared/utils.go Normal file
View File

@@ -0,0 +1,16 @@
package cmdshared
import "strings"
func GetRawForgeVersion(version string) string {
var wantedVersion string
// Check if we have a "-" in the version
if strings.Contains(version, "-") {
// We have a mcVersion-loaderVersion format
// Strip the mcVersion
wantedVersion = strings.Split(version, "-")[1]
} else {
wantedVersion = version
}
return wantedVersion
}