89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package installer
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"skillmgr/internal/config"
|
|
"skillmgr/internal/types"
|
|
)
|
|
|
|
// UninstallSkill 卸载 skill
|
|
func UninstallSkill(name string, platform types.Platform, scope types.Scope) error {
|
|
// 查找记录
|
|
record, err := config.FindInstallRecord(types.ItemTypeSkill, name, platform, scope)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if record == nil {
|
|
return fmt.Errorf("未找到 skill '%s' 的安装记录", name)
|
|
}
|
|
|
|
// 删除目录
|
|
if _, err := os.Stat(record.InstallPath); err == nil {
|
|
if err := os.RemoveAll(record.InstallPath); err != nil {
|
|
return fmt.Errorf("删除目录失败: %w", err)
|
|
}
|
|
}
|
|
|
|
// 移除记录
|
|
if err := config.RemoveInstallRecord(types.ItemTypeSkill, name, platform, scope); err != nil {
|
|
return fmt.Errorf("移除安装记录失败: %w", err)
|
|
}
|
|
|
|
fmt.Printf("✓ Skill '%s' 已卸载\n", name)
|
|
return nil
|
|
}
|
|
|
|
// UninstallCommand 卸载 command
|
|
func UninstallCommand(name string, platform types.Platform, scope types.Scope) error {
|
|
// 查找记录
|
|
record, err := config.FindInstallRecord(types.ItemTypeCommand, name, platform, scope)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if record == nil {
|
|
return fmt.Errorf("未找到 command '%s' 的安装记录", name)
|
|
}
|
|
|
|
// 根据平台决定删除策略
|
|
if platform == types.PlatformClaude {
|
|
// Claude: 删除整个命令组目录
|
|
if _, err := os.Stat(record.InstallPath); err == nil {
|
|
if err := os.RemoveAll(record.InstallPath); err != nil {
|
|
return fmt.Errorf("删除目录失败: %w", err)
|
|
}
|
|
}
|
|
} else if platform == types.PlatformOpenCode {
|
|
// OpenCode: 删除扁平化的命令文件 (<group>-*.md)
|
|
// InstallPath 是 .opencode/command/ 目录
|
|
// 需要删除所有 <name>-*.md 文件
|
|
entries, err := os.ReadDir(record.InstallPath)
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return fmt.Errorf("读取目录失败: %w", err)
|
|
}
|
|
for _, entry := range entries {
|
|
if !entry.IsDir() {
|
|
// 检查文件名是否以 <name>- 开头
|
|
fileName := entry.Name()
|
|
prefix := name + "-"
|
|
if len(fileName) > len(prefix) && fileName[:len(prefix)] == prefix {
|
|
filePath := filepath.Join(record.InstallPath, fileName)
|
|
if err := os.Remove(filePath); err != nil {
|
|
return fmt.Errorf("删除文件 %s 失败: %w", fileName, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 移除记录
|
|
if err := config.RemoveInstallRecord(types.ItemTypeCommand, name, platform, scope); err != nil {
|
|
return fmt.Errorf("移除安装记录失败: %w", err)
|
|
}
|
|
|
|
fmt.Printf("✓ Command '%s' 已卸载\n", name)
|
|
return nil
|
|
}
|