Files
skm/internal/skills/discover.go
T
johannesandClaude Opus 5.5 65a96ada8e feat: skm, pin agent skills from git repos via Nix
CLI (add, remove, list, outdated, update, import) over a skills.json manifest and a skills.lock.json pinned with nix flake prefetch, plus a home-manager module that links each locked skill via builtins.fetchTree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 14:04:50 +02:00

85 lines
1.9 KiB
Go

// Package skills finds skills inside a fetched source.
package skills
import (
"bufio"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
// Skill is a directory containing SKILL.md.
type Skill struct {
// Name comes from the SKILL.md frontmatter, falling back to the directory name.
// Names aren't unique: plugin monorepos ship the same skill in several places.
Name string
// Path is slash-separated and relative to the source root ("." for a repo
// that is itself one skill).
Path string
}
// Discover returns every skill under root in lexical walk order. Skills don't nest.
func Discover(root string) ([]Skill, error) {
var found []Skill
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
if d.Name() == ".git" {
return filepath.SkipDir
}
skillFile := filepath.Join(path, "SKILL.md")
if _, err := os.Stat(skillFile); err != nil {
return nil
}
rel, err := filepath.Rel(root, path)
if err != nil {
return err
}
rel = filepath.ToSlash(rel)
name, err := frontmatterName(skillFile)
if err != nil {
return err
}
if name == "" {
if rel == "." {
return fmt.Errorf("SKILL.md at the repo root has no name in its frontmatter")
}
name = d.Name()
}
found = append(found, Skill{Name: name, Path: rel})
return filepath.SkipDir
})
return found, err
}
// frontmatterName reads `name:` from the YAML frontmatter, or "" if absent.
func frontmatterName(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
sc := bufio.NewScanner(f)
if !sc.Scan() || strings.TrimSpace(sc.Text()) != "---" {
return "", sc.Err()
}
for sc.Scan() {
line := sc.Text()
if strings.TrimSpace(line) == "---" {
break
}
if v, ok := strings.CutPrefix(line, "name:"); ok {
return strings.Trim(strings.TrimSpace(v), `"'`), nil
}
}
return "", sc.Err()
}