119 lines
2.7 KiB
Go
119 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"skillmgr/internal/repo"
|
|
"skillmgr/internal/types"
|
|
)
|
|
|
|
var searchCmd = &cobra.Command{
|
|
Use: "search [keyword]",
|
|
Short: "搜索可用的 skills 和 commands",
|
|
Long: `在已配置的仓库中搜索 skills 和 commands。
|
|
|
|
示例:
|
|
# 搜索所有
|
|
skillmgr search
|
|
|
|
# 按关键字搜索
|
|
skillmgr search kb
|
|
|
|
# 按类型过滤
|
|
skillmgr search --type skill
|
|
|
|
# 按仓库过滤
|
|
skillmgr search --repo lyxy`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
keyword := ""
|
|
if len(args) > 0 {
|
|
keyword = strings.ToLower(args[0])
|
|
}
|
|
|
|
itemTypeStr, _ := cmd.Flags().GetString("type")
|
|
repoFilter, _ := cmd.Flags().GetString("repo")
|
|
|
|
var results []searchResult
|
|
|
|
// 搜索 skills
|
|
if itemTypeStr == "" || itemTypeStr == "skill" {
|
|
skills, err := repo.ListAvailableSkills()
|
|
if err != nil {
|
|
fmt.Printf("警告: 无法获取 skills: %v\n", err)
|
|
} else {
|
|
for _, s := range skills {
|
|
// 按仓库过滤
|
|
if repoFilter != "" && !strings.Contains(strings.ToLower(s.SourceRepo), strings.ToLower(repoFilter)) {
|
|
continue
|
|
}
|
|
// 按关键字过滤
|
|
if keyword == "" || strings.Contains(strings.ToLower(s.Name), keyword) {
|
|
results = append(results, searchResult{
|
|
Type: types.ItemTypeSkill,
|
|
Name: s.Name,
|
|
RepoName: s.SourceRepo,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 搜索 commands
|
|
if itemTypeStr == "" || itemTypeStr == "command" {
|
|
commands, err := repo.ListAvailableCommands()
|
|
if err != nil {
|
|
fmt.Printf("警告: 无法获取 commands: %v\n", err)
|
|
} else {
|
|
for _, c := range commands {
|
|
// 按仓库过滤
|
|
if repoFilter != "" && !strings.Contains(strings.ToLower(c.SourceRepo), strings.ToLower(repoFilter)) {
|
|
continue
|
|
}
|
|
// 按关键字过滤
|
|
if keyword == "" || strings.Contains(strings.ToLower(c.Name), keyword) {
|
|
results = append(results, searchResult{
|
|
Type: types.ItemTypeCommand,
|
|
Name: c.Name,
|
|
RepoName: c.SourceRepo,
|
|
Files: c.Files,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(results) == 0 {
|
|
fmt.Println("未找到匹配项")
|
|
return nil
|
|
}
|
|
|
|
fmt.Printf("找到 %d 个结果:\n\n", len(results))
|
|
for _, r := range results {
|
|
fmt.Printf(" [%s] %s\n", r.Type, r.Name)
|
|
fmt.Printf(" 来源: %s\n", r.RepoName)
|
|
if len(r.Files) > 0 {
|
|
fmt.Printf(" 文件: %s\n", strings.Join(r.Files, ", "))
|
|
}
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
type searchResult struct {
|
|
Type types.ItemType
|
|
Name string
|
|
RepoName string
|
|
Files []string
|
|
}
|
|
|
|
func init() {
|
|
searchCmd.Flags().String("type", "", "过滤类型 (skill|command)")
|
|
searchCmd.Flags().String("repo", "", "过滤仓库")
|
|
|
|
rootCmd.AddCommand(searchCmd)
|
|
}
|