Files
skillshare/internal/resource/skill.go
T
Willie 89fc4fbdf1 feat(sourcewalk): follow first-level source links when enabled
Users link a checkout directly under the skills source (e.g.
skills/_dev-skills -> ~/code/dev-skills) and expect it to behave like any
other first-level entry. sourcewalk resolved only the root, so these links
were invisible to discovery, sync, update, and the dashboard.

follow_source_links (global and project config, default false) enables a
per-operation sourcewalk.Follow policy. Walk, WalkDir, and ReadDir report a
followed link under its own name as a directory and descend into its
target, so names, RelPath, FlatName, SourcePath, tracked-repo detection, and
metadata keys stay logical. Only entries directly under the source root are
followed. The policy is built from config and passed explicitly to every
discovery caller in the CLI, server, and reconcile, so counts agree; the
server keeps a per-instance snapshot refreshed on config load and save
instead of a process-global flag.

A link is skipped, with one warning per operation, when its target is
missing or unreadable, is the source or a parent of it, or overlaps an
active skills target. A missing or unreadable target marks the operation
incomplete: sync then skips pruning and copy-orphan deletion, and reconcile
keeps metadata, so an unmounted drive or a re-clone in progress cannot
erase target entries or install records. A link to an existing file is not
a directory link and stays an ordinary entry; treating it as incomplete
would disable pruning for anyone sharing a file such as .skillignore.

doctor keeps raw link identity: with the key off it still says the link is
not followed and names follow_source_links: true; with it on it reports
followed links and skipped links with the reason.

The raw-walk allowlist gains entries for the policy path in Walk and
WalkDir: the source root walk with a policy, and the walk of a validated
link target whose paths are remapped to the logical link path. Both live
in sourcewalk itself, the package that owns source traversal.

Refs #274
2026-10-05 14:26:09 +08:00

106 lines
2.8 KiB
Go

package resource
import (
"os"
"path/filepath"
"strings"
"skillshare/internal/sourcewalk"
"skillshare/internal/utils"
)
// SkillKind handles directory-based skill resources identified by SKILL.md.
// Walk is the source traversal policy Discover uses; the zero value follows
// no source links.
type SkillKind struct {
Walk sourcewalk.Options
}
var _ ResourceKind = SkillKind{}
func (SkillKind) Kind() string { return "skill" }
// Discover scans sourceDir for directories containing SKILL.md.
// This is a simplified discovery for the resource package; the full
// discovery with ignore support, frontmatter parsing, and context
// collection remains in internal/sync/discover_walk.go.
func (k SkillKind) Discover(sourceDir string) ([]DiscoveredResource, error) {
walkRoot := utils.ResolveSymlink(sourceDir)
var resources []DiscoveredResource
err := sourcewalk.Walk(walkRoot, k.Walk, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if (info.IsDir() && info.Name() == ".git") || sourcewalk.SkipTargetDotDir(walkRoot, path, info) {
return filepath.SkipDir
}
if !info.IsDir() && info.Name() == "SKILL.md" {
skillDir := filepath.Dir(path)
relPath, relErr := filepath.Rel(walkRoot, skillDir)
if relErr != nil || relPath == "." {
return nil
}
relPath = strings.ReplaceAll(relPath, "\\", "/")
name := utils.ParseFrontmatterField(filepath.Join(skillDir, "SKILL.md"), "name")
if name == "" {
name = filepath.Base(skillDir)
}
isInRepo := false
parts := strings.Split(relPath, "/")
if len(parts) > 0 && utils.IsTrackedRepoDir(parts[0]) {
isInRepo = true
}
resources = append(resources, DiscoveredResource{
Name: name,
Kind: "skill",
RelPath: relPath,
AbsPath: skillDir,
IsNested: strings.Contains(relPath, "/"),
FlatName: utils.PathToFlatName(relPath),
IsInRepo: isInRepo,
SourcePath: filepath.Join(sourceDir, relPath),
})
}
return nil
})
if err != nil {
return nil, err
}
return resources, nil
}
// ResolveName reads the name field from SKILL.md frontmatter.
// Falls back to directory base name if frontmatter has no name.
func (SkillKind) ResolveName(path string) string {
skillFile := filepath.Join(path, "SKILL.md")
name := utils.ParseFrontmatterField(skillFile, "name")
if name != "" {
return name
}
return filepath.Base(path)
}
// FlatName converts a relative path to a flat name using __ separator.
func (SkillKind) FlatName(relPath string) string {
return utils.PathToFlatName(relPath)
}
// CreateLink creates a directory symlink from dst pointing to src.
func (SkillKind) CreateLink(src, dst string) error {
return os.Symlink(src, dst)
}
func (SkillKind) SupportsAudit() bool { return true }
func (SkillKind) SupportsTrack() bool { return true }
func (SkillKind) SupportsCollect() bool { return true }