mirror of
https://github.com/packwiz/packwiz.git
synced 2025-10-24 17:44:32 +02:00
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:
154
migrate/loader.go
Normal file
154
migrate/loader.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/packwiz/packwiz/cmdshared"
|
||||
"github.com/packwiz/packwiz/core"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/exp/slices"
|
||||
"os"
|
||||
)
|
||||
|
||||
var loaderCommand = &cobra.Command{
|
||||
Use: "loader [version|latest|recommended]",
|
||||
Short: "Migrate your modloader version to a newer version.",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
modpack, err := core.LoadPack()
|
||||
if err != nil {
|
||||
// Check if it's a no such file or directory error
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println("No pack.toml file found, run 'packwiz init' to create one!")
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Error loading pack: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// Get our current loader, would use the modpack.GetLoaders function
|
||||
// but, it supplements Quilt with Fabric, which isn't needed for this
|
||||
var currentLoaders []string
|
||||
// Add all the keys from modpack.Versions
|
||||
for key := range modpack.Versions {
|
||||
// If it ain't in core.ModLoaders, we don't want it
|
||||
if _, ok := core.ModLoaders[key]; !ok {
|
||||
continue
|
||||
}
|
||||
currentLoaders = append(currentLoaders, key)
|
||||
}
|
||||
// Do some sanity checks on the current loader slice
|
||||
if len(currentLoaders) == 0 {
|
||||
fmt.Println("No loader is currently set in your pack.toml!")
|
||||
os.Exit(1)
|
||||
} else if len(currentLoaders) > 1 {
|
||||
fmt.Println("You have multiple loaders set in your pack.toml, this is not supported!")
|
||||
os.Exit(1)
|
||||
}
|
||||
// Get the Minecraft version for the pack
|
||||
mcVersion, err := modpack.GetMCVersion()
|
||||
if err != nil {
|
||||
fmt.Printf("Error getting Minecraft version: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if args[0] == "latest" {
|
||||
fmt.Println("Updating to latest loader version")
|
||||
// We'll be updating to the latest loader version
|
||||
for _, loader := range currentLoaders {
|
||||
_, latest, gottenLoader := getVersionsForLoader(loader, mcVersion)
|
||||
if !updatePackToVersion(latest, modpack, gottenLoader) {
|
||||
continue
|
||||
}
|
||||
// Write the pack to disk
|
||||
err = modpack.Write()
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing pack.toml: %s\n", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else if args[0] == "recommended" {
|
||||
// TODO: Figure out a way to get the recommended version, this is Forge only
|
||||
// Ensure we're on Forge
|
||||
if !slices.Contains(currentLoaders, "forge") {
|
||||
fmt.Println("The recommended loader version is only available on Forge!")
|
||||
os.Exit(1)
|
||||
}
|
||||
// We'll be updating to the recommended loader version
|
||||
recommendedVer := core.GetForgeRecommended(mcVersion)
|
||||
if recommendedVer == "" {
|
||||
fmt.Println("Error getting recommended Forge version!")
|
||||
os.Exit(1)
|
||||
}
|
||||
if ok := updatePackToVersion(recommendedVer, modpack, core.ModLoaders["forge"]); !ok {
|
||||
os.Exit(1)
|
||||
}
|
||||
// Write the pack to disk
|
||||
err = modpack.Write()
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing pack.toml: %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Updating to explicit loader version")
|
||||
// This one is easy :D
|
||||
versions, _, loader := getVersionsForLoader(currentLoaders[0], mcVersion)
|
||||
// Check if the loader happens to be Forge, since there's two version formats
|
||||
if loader.Name == "forge" {
|
||||
wantedVersion := cmdshared.GetRawForgeVersion(args[0])
|
||||
validateVersion(versions, wantedVersion, loader)
|
||||
_ = updatePackToVersion(wantedVersion, modpack, loader)
|
||||
} else if loader.Name == "liteloader" {
|
||||
// These are weird and just have a MC version
|
||||
fmt.Println("LiteLoader only has 1 version per Minecraft version so we're unable to update!")
|
||||
os.Exit(0)
|
||||
} else {
|
||||
// We're on Fabric or quilt
|
||||
validateVersion(versions, args[0], loader)
|
||||
if ok := updatePackToVersion(args[0], modpack, loader); !ok {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
// Write the pack to disk
|
||||
err = modpack.Write()
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing pack.toml: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
migrateCmd.AddCommand(loaderCommand)
|
||||
}
|
||||
|
||||
func getVersionsForLoader(loader, mcVersion string) ([]string, string, core.ModLoaderComponent) {
|
||||
gottenLoader, ok := core.ModLoaders[loader]
|
||||
if !ok {
|
||||
fmt.Printf("Unknown loader %s\n", loader)
|
||||
os.Exit(1)
|
||||
}
|
||||
versions, latestVersion, err := gottenLoader.VersionListGetter(mcVersion)
|
||||
if err != nil {
|
||||
fmt.Printf("Error getting version list for %s: %s\n", gottenLoader.FriendlyName, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return versions, latestVersion, gottenLoader
|
||||
}
|
||||
|
||||
func validateVersion(versions []string, version string, gottenLoader core.ModLoaderComponent) {
|
||||
if !slices.Contains(versions, version) {
|
||||
fmt.Printf("Version %s is not a valid version for %s\n", version, gottenLoader.FriendlyName)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func updatePackToVersion(version string, modpack core.Pack, loader core.ModLoaderComponent) bool {
|
||||
// Check if the version is already set
|
||||
if version == modpack.Versions[loader.Name] {
|
||||
fmt.Printf("%s is already on version %s!\n", loader.FriendlyName, version)
|
||||
return false
|
||||
}
|
||||
// Set the latest version
|
||||
modpack.Versions[loader.Name] = version
|
||||
fmt.Printf("Updated %s to version %s\n", loader.FriendlyName, version)
|
||||
return true
|
||||
}
|
||||
16
migrate/migrate.go
Normal file
16
migrate/migrate.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"github.com/packwiz/packwiz/cmd"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// migrateCmd represents the base command when called without any subcommands
|
||||
var migrateCmd = &cobra.Command{
|
||||
Use: "migrate [minecraft|loader]",
|
||||
Short: "Migrate your Minecraft and loader versions to newer versions.",
|
||||
}
|
||||
|
||||
func init() {
|
||||
cmd.Add(migrateCmd)
|
||||
}
|
||||
70
migrate/minecraft.go
Normal file
70
migrate/minecraft.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
packCmd "github.com/packwiz/packwiz/cmd"
|
||||
"github.com/packwiz/packwiz/cmdshared"
|
||||
"github.com/packwiz/packwiz/core"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"os"
|
||||
)
|
||||
|
||||
var minecraftCommand = &cobra.Command{
|
||||
Use: "minecraft [version]",
|
||||
Short: "Migrate your Minecraft version to a newer version.",
|
||||
Aliases: []string{"mc"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
modpack, err := core.LoadPack()
|
||||
if err != nil {
|
||||
// Check if it's a no such file or directory error
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Println("No pack.toml file found, run 'packwiz init' to create one!")
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Error loading pack: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
currentVersion, err := modpack.GetMCVersion()
|
||||
if err != nil {
|
||||
fmt.Printf("Error getting Minecraft version from pack: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
wantedMCVersion := args[0]
|
||||
if wantedMCVersion == currentVersion {
|
||||
fmt.Printf("Minecraft version is already %s!\n", wantedMCVersion)
|
||||
os.Exit(0)
|
||||
}
|
||||
mcVersions, err := cmdshared.GetValidMCVersions()
|
||||
if err != nil {
|
||||
fmt.Printf("Error getting Minecraft versions: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
mcVersions.CheckValid(wantedMCVersion)
|
||||
// Set the version in the pack
|
||||
modpack.Versions["minecraft"] = wantedMCVersion
|
||||
// Write the pack to disk
|
||||
err = modpack.Write()
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing pack.toml: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Successfully updated Minecraft version to %s\n", wantedMCVersion)
|
||||
// Prompt the user if they want to update the loader too while they're at it.
|
||||
if cmdshared.PromptYesNo("Would you like to update your loader version to the latest version for this Minecraft version? [Y/n] ") {
|
||||
// We'll run the loader command to update to latest
|
||||
loaderCommand.Run(loaderCommand, []string{"latest"})
|
||||
}
|
||||
// Prompt the user to update their mods too.
|
||||
if cmdshared.PromptYesNo("Would you like to update your mods to the latest versions for this Minecraft version? [Y/n] ") {
|
||||
// Run the update command
|
||||
viper.Set("update.all", true)
|
||||
packCmd.UpdateCmd.Run(packCmd.UpdateCmd, []string{})
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
migrateCmd.AddCommand(minecraftCommand)
|
||||
}
|
||||
Reference in New Issue
Block a user