Skip to content

Commit

Permalink
Add pin and unpin commands/updated update command to work with pin & …
Browse files Browse the repository at this point in the history
…unpin (#234)

* Add pin and unpin commands/updated update command to work with pin & unpin

* Inline cobra functions
  • Loading branch information
joeyak committed Jun 25, 2023
1 parent 0df1998 commit dffdbc9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
90 changes: 90 additions & 0 deletions cmd/pin.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 11 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion core/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"`
Expand Down

0 comments on commit dffdbc9

Please sign in to comment.