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
This commit is contained in:
Willie
2026-03-12 16:45:02 +08:00
parent 7a6e78783b
commit cc93c4f0a4
4 changed files with 64 additions and 7 deletions
+45 -7
View File
@@ -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 <name> [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`)
+8
View File
@@ -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 == "" {
+6
View File
@@ -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)})
}
+5
View File
@@ -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")
}