DiscoverLocalSkillsWithOptions finds skills in a local directory using the same conventions as remote discovery, with configurable discovery behavior.
(dir string, opts DiscoverOptions)
| 972 | // DiscoverLocalSkillsWithOptions finds skills in a local directory using the |
| 973 | // same conventions as remote discovery, with configurable discovery behavior. |
| 974 | func DiscoverLocalSkillsWithOptions(dir string, opts DiscoverOptions) ([]Skill, error) { |
| 975 | absDir, err := filepath.Abs(dir) |
| 976 | if err != nil { |
| 977 | return nil, fmt.Errorf("could not resolve path: %w", err) |
| 978 | } |
| 979 | |
| 980 | info, err := os.Stat(absDir) |
| 981 | if err != nil { |
| 982 | return nil, fmt.Errorf("could not access %s: %w", dir, err) |
| 983 | } |
| 984 | if !info.IsDir() { |
| 985 | return nil, fmt.Errorf("%s is not a directory", dir) |
| 986 | } |
| 987 | |
| 988 | if _, err := os.Stat(filepath.Join(absDir, "SKILL.md")); err == nil { |
| 989 | skill, err := localSkillFromDir(absDir) |
| 990 | if err != nil { |
| 991 | return nil, err |
| 992 | } |
| 993 | skill.Path = "." |
| 994 | return []Skill{*skill}, nil |
| 995 | } |
| 996 | |
| 997 | var skills []Skill |
| 998 | seen := make(map[string]bool) |
| 999 | |
| 1000 | err = filepath.Walk(absDir, func(p string, info os.FileInfo, walkErr error) error { |
| 1001 | if walkErr != nil { |
| 1002 | return walkErr |
| 1003 | } |
| 1004 | // Skip symlinks to avoid following links outside the source tree. |
| 1005 | if info.Mode()&os.ModeSymlink != 0 { |
| 1006 | return nil |
| 1007 | } |
| 1008 | if info.IsDir() || info.Name() != "SKILL.md" { |
| 1009 | return nil |
| 1010 | } |
| 1011 | |
| 1012 | relPath, relErr := filepath.Rel(absDir, p) |
| 1013 | if relErr != nil { |
| 1014 | return relErr |
| 1015 | } |
| 1016 | relPath = filepath.ToSlash(relPath) |
| 1017 | |
| 1018 | entry := treeEntry{Path: relPath, Type: "blob"} |
| 1019 | m := matchSkillConventions(entry) |
| 1020 | if m == nil { |
| 1021 | m = matchHiddenDirConventions(entry) |
| 1022 | } |
| 1023 | if m == nil { |
| 1024 | return nil |
| 1025 | } |
| 1026 | if seen[m.skillDir] { |
| 1027 | return nil |
| 1028 | } |
| 1029 | seen[m.skillDir] = true |
| 1030 | |
| 1031 | skill, skillErr := localSkillFromDir(filepath.Join(absDir, filepath.FromSlash(m.skillDir))) |