-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
major(release): add remove command to uninstall addons
- Loading branch information
Showing
4 changed files
with
91 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/m-triassi/wowforge-cli/internal/files" | ||
"github.com/m-triassi/wowforge-cli/internal/search" | ||
"github.com/m-triassi/wowforge-cli/pkg/curseforge" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
// removeCmd represents the remove command | ||
var removeCmd = &cobra.Command{ | ||
Use: "remove <addon id>", | ||
Short: "Remove the specified addon from game install", | ||
Long: `Removes the passed addon id from the tracked list of addons, and attempts to delete | ||
the associated files for that addon.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
modId, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
panic(fmt.Errorf("passed mod ID is not strictly an integer: %w", err)) | ||
} | ||
|
||
addons := viper.GetIntSlice("addons") | ||
remove := search.Find(addons, modId) | ||
addons = append(addons[:remove], addons[remove+1:]...) | ||
|
||
fileSet, _ := curseforge.GetFiles(modId) | ||
filename := curseforge.NegotiateFile(fileSet).Filename | ||
|
||
re := regexp.MustCompile("[a-zA-Z]*") | ||
res := string(re.Find([]byte(filename))) | ||
|
||
installPath := viper.GetString("install") | ||
del, err := filepath.Glob(installPath + res + "*") | ||
|
||
if err != nil { | ||
panic(fmt.Errorf("could not read filesystem at path (%s): %w", installPath, err)) | ||
} | ||
|
||
filesystem.DeleteAll(del) | ||
|
||
viper.Set("addons", addons) | ||
viper.WriteConfig() | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(removeCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package filesystem | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func Delete(filename string) { | ||
err := os.RemoveAll(filename) | ||
if err != nil { | ||
panic(fmt.Errorf("could not find target file to delete: %w", err)) | ||
} | ||
} | ||
|
||
func DeleteAll(targets []string) { | ||
for _, target := range targets { | ||
Delete(target) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package search | ||
|
||
func Contains(haystack []int, needle int) bool { | ||
return Find(haystack, needle) >= 0 | ||
} | ||
|
||
func Find(haystack []int, needle int) int { | ||
for index, value := range haystack { | ||
if value == needle { | ||
return index | ||
} | ||
} | ||
return -1 | ||
} |