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

View File

@@ -2,19 +2,16 @@ package cmd
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/fatih/camelcase"
"github.com/igorsobreira/titlecase"
"github.com/packwiz/packwiz/cmdshared"
"github.com/packwiz/packwiz/core"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"path/filepath"
"strings"
)
// initCmd represents the init command
@@ -59,7 +56,7 @@ var initCmd = &cobra.Command{
version = initReadValue("Version [1.0.0]: ", "1.0.0")
}
mcVersions, err := getValidMCVersions()
mcVersions, err := cmdshared.GetValidMCVersions()
if err != nil {
fmt.Printf("Failed to get latest minecraft versions: %s\n", err)
os.Exit(1)
@@ -79,7 +76,7 @@ var initCmd = &cobra.Command{
mcVersion = initReadValue("Minecraft version ["+latestVersion+"]: ", latestVersion)
}
}
mcVersions.checkValid(mcVersion)
mcVersions.CheckValid(mcVersion)
modLoaderName := strings.ToLower(viper.GetString("init.modloader"))
if len(modLoaderName) == 0 {
@@ -239,45 +236,3 @@ func initReadValue(prompt string, def string) string {
}
return def
}
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
}

View File

@@ -9,8 +9,8 @@ import (
"os"
)
// updateCmd represents the update command
var updateCmd = &cobra.Command{
// UpdateCmd represents the update command
var UpdateCmd = &cobra.Command{
Use: "update [name]",
Short: "Update an external file (or all external files) in the modpack",
Aliases: []string{"upgrade"},
@@ -210,8 +210,8 @@ var updateCmd = &cobra.Command{
}
func init() {
rootCmd.AddCommand(updateCmd)
rootCmd.AddCommand(UpdateCmd)
updateCmd.Flags().BoolP("all", "a", false, "Update all external files")
_ = viper.BindPFlag("update.all", updateCmd.Flags().Lookup("all"))
UpdateCmd.Flags().BoolP("all", "a", false, "Update all external files")
_ = viper.BindPFlag("update.all", UpdateCmd.Flags().Lookup("all"))
}