Add --side argument to filter list command (#256)

* added list command filter by side

* changed error logging for --side flags

* removed len checks on side flags

* removed comments previous commit made useless
This commit is contained in:
JaegyuDev 2023-09-16 12:26:10 -04:00 committed by GitHub
parent b451a9b034
commit 9889b48b0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View File

@ -39,6 +39,24 @@ var listCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
// Filter mods by side
if viper.IsSet("list.side") {
side := viper.GetString("list.side")
if side != core.UniversalSide && side != core.ServerSide && side != core.ClientSide {
fmt.Printf("Invalid side %q, must be one of client, server, or both (default)\n", side)
os.Exit(1)
}
i := 0
for _, mod := range mods {
if mod.Side == side || mod.Side == core.EmptySide || mod.Side == core.UniversalSide || side == core.UniversalSide {
mods[i] = mod
i++
}
}
mods = mods[:i]
}
sort.Slice(mods, func(i, j int) bool { sort.Slice(mods, func(i, j int) bool {
return strings.ToLower(mods[i].Name) < strings.ToLower(mods[j].Name) return strings.ToLower(mods[i].Name) < strings.ToLower(mods[j].Name)
}) })
@ -61,4 +79,7 @@ func init() {
listCmd.Flags().BoolP("version", "v", false, "Print name and version") listCmd.Flags().BoolP("version", "v", false, "Print name and version")
_ = viper.BindPFlag("list.version", listCmd.Flags().Lookup("version")) _ = viper.BindPFlag("list.version", listCmd.Flags().Lookup("version"))
listCmd.Flags().StringP("side", "s", "", "Filter mods by side (e.g., client or server)")
_ = viper.BindPFlag("list.side", listCmd.Flags().Lookup("side"))
} }

View File

@ -4,13 +4,14 @@ import (
"archive/zip" "archive/zip"
"bufio" "bufio"
"fmt" "fmt"
"os"
"strconv"
"github.com/packwiz/packwiz/cmdshared" "github.com/packwiz/packwiz/cmdshared"
"github.com/packwiz/packwiz/core" "github.com/packwiz/packwiz/core"
"github.com/packwiz/packwiz/curseforge/packinterop" "github.com/packwiz/packwiz/curseforge/packinterop"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"os"
"strconv"
) )
// exportCmd represents the export command // exportCmd represents the export command
@ -20,8 +21,8 @@ var exportCmd = &cobra.Command{
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
side := viper.GetString("curseforge.export.side") side := viper.GetString("curseforge.export.side")
if len(side) == 0 || (side != core.UniversalSide && side != core.ServerSide && side != core.ClientSide) { if side != core.UniversalSide && side != core.ServerSide && side != core.ClientSide {
fmt.Println("Invalid side!") fmt.Printf("Invalid side %q, must be one of client, server, or both (default)\n", side)
os.Exit(1) os.Exit(1)
} }