From 264a4e01fc51ddfc67226bcfe2a4392a9e985255 Mon Sep 17 00:00:00 2001 From: Massimo Triassi Date: Tue, 19 Dec 2023 19:18:43 -0500 Subject: [PATCH 1/7] refactor: #17 extract contains to internal class for reuse --- cmd/add.go | 12 ++---------- internal/search/search.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 internal/search/search.go diff --git a/cmd/add.go b/cmd/add.go index 9bd12f7..ce2c901 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -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" @@ -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() @@ -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 -} diff --git a/internal/search/search.go b/internal/search/search.go new file mode 100644 index 0000000..ed01b4e --- /dev/null +++ b/internal/search/search.go @@ -0,0 +1,10 @@ +package search + +func Contains(haystack []int, needle int) bool { + for _, value := range haystack { + if value == needle { + return true + } + } + return false +} From e3f1194a819e533d0e11bfa80c3dbe397868dba2 Mon Sep 17 00:00:00 2001 From: Massimo Triassi Date: Tue, 19 Dec 2023 19:26:40 -0500 Subject: [PATCH 2/7] feat: #17 add find command and deduplicate functionality --- internal/search/search.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/search/search.go b/internal/search/search.go index ed01b4e..d4cbe1f 100644 --- a/internal/search/search.go +++ b/internal/search/search.go @@ -1,10 +1,14 @@ package search func Contains(haystack []int, needle int) bool { - for _, value := range haystack { + return Find(haystack, needle) <= 0 +} + +func Find(haystack []int, needle int) int { + for index, value := range haystack { if value == needle { - return true + return index } } - return false + return -1 } From 58c59b84433274986026511fbcf0ed67a14045d2 Mon Sep 17 00:00:00 2001 From: Massimo Triassi Date: Tue, 19 Dec 2023 19:56:26 -0500 Subject: [PATCH 3/7] fix: #17 reverse equality sign to get correct result --- internal/search/search.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/search/search.go b/internal/search/search.go index d4cbe1f..c8a15c1 100644 --- a/internal/search/search.go +++ b/internal/search/search.go @@ -1,7 +1,7 @@ package search func Contains(haystack []int, needle int) bool { - return Find(haystack, needle) <= 0 + return Find(haystack, needle) >= 0 } func Find(haystack []int, needle int) int { From d4d287b3a9b535a454a2298be92a331fb9cfdb2b Mon Sep 17 00:00:00 2001 From: Massimo Triassi Date: Tue, 19 Dec 2023 21:22:21 -0500 Subject: [PATCH 4/7] feat: #17 add basic filesystem package for managing folders --- internal/files/filesystem.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 internal/files/filesystem.go diff --git a/internal/files/filesystem.go b/internal/files/filesystem.go new file mode 100644 index 0000000..bb8ca45 --- /dev/null +++ b/internal/files/filesystem.go @@ -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) + } +} From 0ff7045383be7c02e7f454147cd3c72271bceebe Mon Sep 17 00:00:00 2001 From: Massimo Triassi Date: Tue, 19 Dec 2023 21:23:05 -0500 Subject: [PATCH 5/7] feat: #17 add command to remove installed addons --- cmd/remove.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 cmd/remove.go diff --git a/cmd/remove.go b/cmd/remove.go new file mode 100644 index 0000000..4c56e4c --- /dev/null +++ b/cmd/remove.go @@ -0,0 +1,50 @@ +/* +Copyright © 2023 NAME HERE +*/ +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 ", + 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))) + + del, err := filepath.Glob(viper.GetString("install") + res + "*") + + filesystem.DeleteAll(del) + + viper.Set("addons", addons) + viper.WriteConfig() + }, +} + +func init() { + rootCmd.AddCommand(removeCmd) +} From ffb784a61aa67030880380c134fd22946bac3bff Mon Sep 17 00:00:00 2001 From: Massimo Triassi Date: Tue, 19 Dec 2023 21:32:52 -0500 Subject: [PATCH 6/7] fix: #17 add error handling to prevent trying to delete bad directories --- cmd/remove.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd/remove.go b/cmd/remove.go index 4c56e4c..8bea1cb 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -33,13 +33,18 @@ the associated files for that addon.`, fileSet, _ := curseforge.GetFiles(modId) filename := curseforge.NegotiateFile(fileSet).Filename + re := regexp.MustCompile("[a-zA-Z]*") res := string(re.Find([]byte(filename))) - del, err := filepath.Glob(viper.GetString("install") + res + "*") + installPath := viper.GetString("install") + del, err := filepath.Glob(installPath + res + "*") - filesystem.DeleteAll(del) + 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() }, From dd81b7f887163e63f708920add35aa4273796a62 Mon Sep 17 00:00:00 2001 From: Massimo Triassi Date: Tue, 19 Dec 2023 21:44:14 -0500 Subject: [PATCH 7/7] major: #17 finalize remove mostly testing if this commit type will increment major version in release --- cmd/remove.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/remove.go b/cmd/remove.go index 8bea1cb..49b4133 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -45,6 +45,7 @@ the associated files for that addon.`, } filesystem.DeleteAll(del) + viper.Set("addons", addons) viper.WriteConfig() },