Skip to content

Commit

Permalink
major(release): add remove command to uninstall addons
Browse files Browse the repository at this point in the history
  • Loading branch information
m-triassi authored Dec 20, 2023
2 parents 57f8983 + dd81b7f commit 3dccc07
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 10 deletions.
12 changes: 2 additions & 10 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"fmt"
"github.com/m-triassi/wowforge-cli/internal/search"
"github.com/m-triassi/wowforge-cli/pkg/curseforge"
"github.com/spf13/viper"
"strconv"
Expand Down Expand Up @@ -46,7 +47,7 @@ update that addon in isolation.`,
}

list := viper.GetIntSlice("addons")
if !contains(list, modId) {
if !search.Contains(list, modId) {
list = append(list, modId)
viper.Set("addons", list)
viper.WriteConfig()
Expand All @@ -57,12 +58,3 @@ update that addon in isolation.`,
func init() {
rootCmd.AddCommand(addCmd)
}

func contains(haystack []int, needle int) bool {
for _, value := range haystack {
if value == needle {
return true
}
}
return false
}
56 changes: 56 additions & 0 deletions cmd/remove.go
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)
}
19 changes: 19 additions & 0 deletions internal/files/filesystem.go
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)
}
}
14 changes: 14 additions & 0 deletions internal/search/search.go
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
}

0 comments on commit 3dccc07

Please sign in to comment.