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
This commit is contained in:
joeyak 2023-06-24 20:38:03 -04:00 committed by GitHub
parent 0df199852f
commit dffdbc9b80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 104 additions and 2 deletions

90
cmd/pin.go Normal file
View File

@ -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)
}

View File

@ -2,11 +2,12 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
"github.com/packwiz/packwiz/cmdshared" "github.com/packwiz/packwiz/cmdshared"
"github.com/packwiz/packwiz/core" "github.com/packwiz/packwiz/core"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"os"
) )
// UpdateCmd represents the update command // UpdateCmd represents the update command
@ -77,6 +78,11 @@ var UpdateCmd = &cobra.Command{
continue continue
} }
if check.UpdateAvailable { if check.UpdateAvailable {
if v[i].Pin {
fmt.Printf("Update skipped for pinned mod %s\n", v[i].Name)
continue
}
if !updatesFound { if !updatesFound {
fmt.Println("Updates found:") fmt.Println("Updates found:")
updatesFound = true updatesFound = true
@ -133,6 +139,10 @@ var UpdateCmd = &cobra.Command{
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
if modData.Pin {
fmt.Println("Version is pinned; run the unpin command to allow updating")
os.Exit(1)
}
singleUpdatedName = modData.Name singleUpdatedName = modData.Name
updaterFound := false updaterFound := false
for k := range modData.Update { for k := range modData.Update {

View File

@ -2,12 +2,13 @@ package core
import ( import (
"errors" "errors"
"github.com/BurntSushi/toml"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"github.com/BurntSushi/toml"
) )
// Mod stores metadata about a mod. This is written to a TOML file for each mod. // 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"` Name string `toml:"name"`
FileName string `toml:"filename"` FileName string `toml:"filename"`
Side string `toml:"side,omitempty"` Side string `toml:"side,omitempty"`
Pin bool `toml:"pin,omitempty"`
Download ModDownload `toml:"download"` 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 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"` Update map[string]map[string]interface{} `toml:"update"`