From dffdbc9b809c2aa236588d9fe3af0f10c220c0fa Mon Sep 17 00:00:00 2001 From: joeyak <27922090+joeyak@users.noreply.github.com> Date: Sat, 24 Jun 2023 20:38:03 -0400 Subject: [PATCH] Add pin and unpin commands/updated update command to work with pin & unpin (#234) * Add pin and unpin commands/updated update command to work with pin & unpin * Inline cobra functions --- cmd/pin.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/update.go | 12 ++++++- core/mod.go | 4 ++- 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 cmd/pin.go diff --git a/cmd/pin.go b/cmd/pin.go new file mode 100644 index 0000000..be4d8c8 --- /dev/null +++ b/cmd/pin.go @@ -0,0 +1,90 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/packwiz/packwiz/core" + "github.com/spf13/cobra" +) + +func pinMod(cmd *cobra.Command, args []string, pinned bool) { + fmt.Println("Loading modpack...") + pack, err := core.LoadPack() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + index, err := pack.LoadIndex() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + modPath, ok := index.FindMod(args[0]) + if !ok { + fmt.Println("Can't find this file; please ensure you have run packwiz refresh and use the name of the .pw.toml file (defaults to the project slug)") + os.Exit(1) + } + modData, err := core.LoadMod(modPath) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + modData.Pin = pinned + format, hash, err := modData.Write() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + err = index.RefreshFileWithHash(modPath, format, hash, true) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + err = index.Write() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + err = pack.UpdateIndexHash() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + err = pack.Write() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + message := "pinned" + if !pinned { + message = "unpinned" + } + fmt.Printf("%s %s successfully!\n", args[0], message) +} + +// pinCmd represents the pin command +var pinCmd = &cobra.Command{ + Use: "pin", + Short: "Pin a mod to the current version", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + pinMod(cmd, args, true) + }, +} + +// unpinCmd represents the unpin command +var unpinCmd = &cobra.Command{ + Use: "unpin", + Short: "Unpin a mod from the current version", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + pinMod(cmd, args, false) + }, +} + +func init() { + rootCmd.AddCommand(pinCmd) + rootCmd.AddCommand(unpinCmd) +} diff --git a/cmd/update.go b/cmd/update.go index fa89216..af17182 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -2,11 +2,12 @@ package cmd import ( "fmt" + "os" + "github.com/packwiz/packwiz/cmdshared" "github.com/packwiz/packwiz/core" "github.com/spf13/cobra" "github.com/spf13/viper" - "os" ) // UpdateCmd represents the update command @@ -77,6 +78,11 @@ var UpdateCmd = &cobra.Command{ continue } if check.UpdateAvailable { + if v[i].Pin { + fmt.Printf("Update skipped for pinned mod %s\n", v[i].Name) + continue + } + if !updatesFound { fmt.Println("Updates found:") updatesFound = true @@ -133,6 +139,10 @@ var UpdateCmd = &cobra.Command{ fmt.Println(err) os.Exit(1) } + if modData.Pin { + fmt.Println("Version is pinned; run the unpin command to allow updating") + os.Exit(1) + } singleUpdatedName = modData.Name updaterFound := false for k := range modData.Update { diff --git a/core/mod.go b/core/mod.go index 05a6f33..c9278e0 100644 --- a/core/mod.go +++ b/core/mod.go @@ -2,12 +2,13 @@ package core import ( "errors" - "github.com/BurntSushi/toml" "io" "os" "path/filepath" "regexp" "strings" + + "github.com/BurntSushi/toml" ) // Mod stores metadata about a mod. This is written to a TOML file for each mod. @@ -16,6 +17,7 @@ type Mod struct { Name string `toml:"name"` FileName string `toml:"filename"` Side string `toml:"side,omitempty"` + Pin bool `toml:"pin,omitempty"` Download ModDownload `toml:"download"` // Update is a map of map of stuff, so you can store arbitrary values on string keys to define updating Update map[string]map[string]interface{} `toml:"update"`