From cc93c4f0a4337c24f64623344fe51687135dab2a Mon Sep 17 00:00:00 2001 From: Willie Date: Thu, 12 Mar 2026 14:08:27 +0800 Subject: [PATCH] fix(extras): add y/N confirm prompt to remove, clean up empty dirs - extras remove: replace silent-exit warning with interactive [y/N] prompt; --force skips the prompt as before - extras remove: after removing config entry, delete the source directory if empty and the parent extras/ directory if empty - sync extras: clean up empty extras/ directory when no extras are configured (both global and project mode) - Add removeEmptyDir helper and ExtrasParentDirProject path function --- cmd/skillshare/extras_remove.go | 52 ++++++++++++++++++++++++++++----- cmd/skillshare/helpers.go | 8 +++++ cmd/skillshare/sync_extras.go | 6 ++++ internal/config/extras_path.go | 5 ++++ 4 files changed, 64 insertions(+), 7 deletions(-) diff --git a/cmd/skillshare/extras_remove.go b/cmd/skillshare/extras_remove.go index b530283dd..6beed99fd 100644 --- a/cmd/skillshare/extras_remove.go +++ b/cmd/skillshare/extras_remove.go @@ -3,6 +3,8 @@ package main import ( "fmt" "os" + "path/filepath" + "strings" "time" "skillshare/internal/config" @@ -82,8 +84,13 @@ func extrasRemoveGlobal(name string, force bool, start time.Time) error { ui.Info("Source files in %s will NOT be deleted.", shortenPath(sourceDir)) ui.Info("Existing symlinks in targets will become orphaned.") fmt.Println() - ui.Info("Use --force to skip this warning.") - return nil + fmt.Print("Remove? [y/N]: ") + var input string + fmt.Scanln(&input) + if input = strings.ToLower(strings.TrimSpace(input)); input != "y" && input != "yes" { + ui.Info("Cancelled.") + return nil + } } // Remove from slice @@ -93,7 +100,11 @@ func extrasRemoveGlobal(name string, force bool, start time.Time) error { } ui.Success("Removed %q from extras config", name) - ui.Info("Source files were preserved. Run 'skillshare sync extras' to clean up orphaned links.") + + sourceDir := config.ExtrasSourceDir(cfg.Source, name) + cleanEmptyExtrasDir(sourceDir) + + ui.Info("Run 'skillshare sync extras' to clean up orphaned links.") // Oplog e := oplog.NewEntry("extras-remove", "ok", time.Since(start)) @@ -127,8 +138,13 @@ func extrasRemoveProject(cwd, name string, force bool, start time.Time) error { ui.Info("Source files in %s will NOT be deleted.", shortenPath(sourceDir)) ui.Info("Existing symlinks in targets will become orphaned.") fmt.Println() - ui.Info("Use --force to skip this warning.") - return nil + fmt.Print("Remove? [y/N]: ") + var input string + fmt.Scanln(&input) + if input = strings.ToLower(strings.TrimSpace(input)); input != "y" && input != "yes" { + ui.Info("Cancelled.") + return nil + } } // Remove from slice @@ -138,7 +154,11 @@ func extrasRemoveProject(cwd, name string, force bool, start time.Time) error { } ui.Success("Removed %q from project extras config", name) - ui.Info("Source files were preserved. Run 'skillshare sync extras -p' to clean up orphaned links.") + + sourceDir := config.ExtrasSourceDirProject(cwd, name) + cleanEmptyExtrasDir(sourceDir) + + ui.Info("Run 'skillshare sync extras -p' to clean up orphaned links.") // Oplog cfgPath := config.ProjectConfigPath(cwd) @@ -149,6 +169,24 @@ func extrasRemoveProject(cwd, name string, force bool, start time.Time) error { return nil } +// cleanEmptyExtrasDir removes the source directory if it exists and is empty. +// Also removes the parent extras/ directory if it becomes empty. +func cleanEmptyExtrasDir(dir string) { + entries, err := os.ReadDir(dir) + if err != nil { + return // doesn't exist or unreadable — nothing to do + } + if len(entries) == 0 { + os.Remove(dir) + ui.Info("Removed empty source directory %s", shortenPath(dir)) + } else { + ui.Info("Source files preserved in %s (%d files)", shortenPath(dir), len(entries)) + } + + // Clean parent extras/ directory if empty + removeEmptyDir(filepath.Dir(dir)) +} + func printExtrasRemoveHelp() { fmt.Println(`Usage: skillshare extras remove [options] @@ -161,7 +199,7 @@ Arguments: name Name of the extra to remove Options: - --force, -f Skip confirmation warning + --force, -f Skip confirmation prompt --project, -p Remove from project config (.skillshare/) --global, -g Remove from global config (~/.config/skillshare/) --help, -h Show this help`) diff --git a/cmd/skillshare/helpers.go b/cmd/skillshare/helpers.go index 8813c85f6..469907da8 100644 --- a/cmd/skillshare/helpers.go +++ b/cmd/skillshare/helpers.go @@ -29,6 +29,14 @@ func pathExists(path string) bool { return err == nil } +// removeEmptyDir removes a directory only if it exists and is empty. +func removeEmptyDir(dir string) { + entries, err := os.ReadDir(dir) + if err == nil && len(entries) == 0 { + os.Remove(dir) + } +} + // capitalize returns s with the first Unicode character upper-cased. func capitalize(s string) string { if s == "" { diff --git a/cmd/skillshare/sync_extras.go b/cmd/skillshare/sync_extras.go index 6c6c678b3..0a723e1a5 100644 --- a/cmd/skillshare/sync_extras.go +++ b/cmd/skillshare/sync_extras.go @@ -66,6 +66,9 @@ func cmdSyncExtrasGlobal(dryRun, force, jsonOutput bool, start time.Time) error } if len(cfg.Extras) == 0 { + // Clean up empty extras directory + removeEmptyDir(config.ExtrasParentDir(cfg.Source)) + if jsonOutput { return writeJSON(&syncExtrasJSONOutput{Extras: []syncExtrasJSONEntry{}, Duration: formatDuration(start)}) } @@ -218,6 +221,9 @@ func cmdSyncExtrasProject(cwd string, dryRun, force, jsonOutput bool, start time } if len(projCfg.Extras) == 0 { + // Clean up empty extras directory + removeEmptyDir(config.ExtrasParentDirProject(cwd)) + if jsonOutput { return writeJSON(&syncExtrasJSONOutput{Extras: []syncExtrasJSONEntry{}, Duration: formatDuration(start)}) } diff --git a/internal/config/extras_path.go b/internal/config/extras_path.go index 9134c07f4..1287cbabd 100644 --- a/internal/config/extras_path.go +++ b/internal/config/extras_path.go @@ -16,3 +16,8 @@ func ExtrasSourceDirProject(projectRoot, name string) string { func ExtrasParentDir(skillsSource string) string { return filepath.Join(filepath.Dir(skillsSource), "extras") } + +// ExtrasParentDirProject returns the extras parent directory in project mode. +func ExtrasParentDirProject(projectRoot string) string { + return filepath.Join(projectRoot, ".skillshare", "extras") +}