Skip to content

Commit 4e27e93

Browse files
authored
Merge pull request #1 from DeadFrostt/codex/implement-delete-pack-handler
Cleanup state when deleting packs
2 parents da97fd3 + 758df3e commit 4e27e93

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

main.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var (
2424
autoYes bool
2525
mcVersionFlag string // override MC version
2626
loaderFlag string // override loader
27-
verbose bool // enable verbose logging
27+
verbose bool // enable verbose logging
2828
)
2929

3030
func main() {
@@ -278,12 +278,33 @@ func main() {
278278
if err := SaveConfig(cfgFile, cfg); err != nil {
279279
return err
280280
}
281-
fmt.Printf("Deleted modpack %q\n", name)
282-
// Also remove associated state and mods directory?
283-
// state, _ := LoadState(stateFile)
284-
// delete(state, name)
285-
// SaveState(stateFile, state)
286-
// os.RemoveAll(filepath.Join(modsDir, name))
281+
fmt.Printf("Deleted modpack %q from config\n", name)
282+
283+
// --- Remove state entry ---
284+
state, err := LoadState(stateFile)
285+
if err != nil {
286+
fmt.Printf("Warning: could not load state file: %v\n", err)
287+
} else {
288+
if _, ok := state[name]; ok {
289+
delete(state, name)
290+
if err := SaveState(stateFile, state); err != nil {
291+
fmt.Printf("Warning: could not save updated state file: %v\n", err)
292+
} else if verbose {
293+
fmt.Printf("Removed state for %q\n", name)
294+
}
295+
} else if verbose {
296+
fmt.Printf("No state found for %q\n", name)
297+
}
298+
}
299+
300+
// --- Remove mods directory ---
301+
dir := filepath.Join(modsDir, name)
302+
if err := os.RemoveAll(dir); err != nil {
303+
fmt.Printf("Warning: failed to remove mods directory %s: %v\n", dir, err)
304+
} else if verbose {
305+
fmt.Printf("Removed mods directory %s\n", dir)
306+
}
307+
287308
return nil
288309
},
289310
}
@@ -436,7 +457,7 @@ func main() {
436457
continue
437458
}
438459

439-
// --- Perform Download ---
460+
// --- Perform Download ---
440461

441462
// Remove old file ONLY if it exists AND the new filename is different
442463
if fileExists && expectedFilePath != "" && modState.Filename != ver.Files[0].Filename {
@@ -490,7 +511,7 @@ func main() {
490511

491512
// check-updates
492513
checkUpdatesCmd := &cobra.Command{
493-
Use: "check-updates [modpack]", // Renamed from "status"
514+
Use: "check-updates [modpack]", // Renamed from "status"
494515
Short: "Check Modrinth for newer versions of mods in a modpack", // Updated description
495516
Args: cobra.ExactArgs(1),
496517
RunE: func(cmd *cobra.Command, args []string) error {

0 commit comments

Comments
 (0)