Files
skillshare/internal/sync/skill_run.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

151 lines
5.1 KiB
Go

package sync
import (
"errors"
"fmt"
"os"
"skillshare/internal/config"
"skillshare/internal/sourcewalk"
"skillshare/internal/utils"
)
// ErrSymlinkConflict marks a symlink-mode target whose link points somewhere
// other than the source. Force replaces the link.
var ErrSymlinkConflict = errors.New("conflict")
// SkillTarget is one target a skill sync visits.
type SkillTarget struct {
Name string
Target config.TargetConfig
Mode string // resolved skills mode: merge, copy, or anything else for symlink
}
// SkillRunOptions holds what differs between skill sync callers.
type SkillRunOptions struct {
Source string // skills source directory
ProjectRoot string // "" in global mode
IgnorePatterns []string
DryRun bool
Force bool
// SourceIncomplete means a followed source link could not be read, so
// skills are missing from the list without being removed. Nothing is
// pruned; linking and copying still run.
SourceIncomplete bool
// OnProgress reports copy-mode progress; nil reports nothing.
OnProgress func(current, total int, skill string)
}
// SkillTargetResult is the outcome of a skill sync for one target.
type SkillTargetResult struct {
Name string
Mode string
Linked []string // merge: linked; copy: copied
Updated []string
Skipped []string
Pruned []string // nil when no prune ran or it returned nothing
// LocalDirs are user folders a merge prune kept.
LocalDirs []string
DirCreated string
// SymlinkStatus is the target path's status before a symlink-mode sync.
SymlinkStatus TargetStatus
Warnings []string // unmatched includes, prune failure, then the prune's own warnings
Err error // the target failed to sync; nothing else is set
}
// SyncSkillTarget links, copies, or symlinks skills into one target, then
// prunes orphans left by earlier syncs. It prints nothing and writes no oplog.
func SyncSkillTarget(t SkillTarget, skills []DiscoveredSkill, opts SkillRunOptions) SkillTargetResult {
res := SkillTargetResult{Name: t.Name, Mode: t.Mode}
sc := t.Target.SkillsConfig()
switch t.Mode {
case "merge":
result, err := SyncTargetMergeWithSkills(t.Name, t.Target, skills, opts.Source, opts.DryRun, opts.Force, opts.ProjectRoot)
if err != nil {
res.Err = err
return res
}
res.Linked, res.Updated, res.Skipped, res.DirCreated = result.Linked, result.Updated, result.Skipped, result.DirCreated
res.addTargetWarnings(result.Warnings)
if opts.SourceIncomplete {
break
}
prune, err := PruneOrphanLinksWithSkills(PruneOptions{
TargetPath: sc.Path, SourcePath: opts.Source, Skills: skills,
Include: sc.Include, Exclude: sc.Exclude, TargetNaming: sc.TargetNaming, TargetName: t.Name,
DryRun: opts.DryRun, Force: opts.Force,
})
res.addPrune(prune, err)
if prune != nil {
res.LocalDirs = prune.LocalDirs
}
case "copy":
result, err := SyncTargetCopyWithSkillsOptions(t.Name, t.Target, skills, opts.Source, opts.DryRun, opts.Force, opts.OnProgress, CopyOptions{IgnorePatterns: opts.IgnorePatterns})
if err != nil {
res.Err = err
return res
}
res.Linked, res.Updated, res.Skipped, res.DirCreated = result.Copied, result.Updated, result.Skipped, result.DirCreated
res.addTargetWarnings(result.Warnings)
if opts.SourceIncomplete {
break
}
prune, err := PruneOrphanCopiesWithSkills(sc.Path, skills, sc.Include, sc.Exclude, t.Name, sc.TargetNaming, opts.DryRun)
res.addPrune(prune, err)
default:
res.SymlinkStatus = CheckStatus(sc.Path, opts.Source)
// A symlink pointing elsewhere is replaced only with force.
if res.SymlinkStatus == StatusConflict {
if !opts.Force {
link, err := utils.ResolveLinkTarget(sc.Path)
if err != nil {
link = "(unable to resolve target)"
}
res.Err = fmt.Errorf("%w - symlink points to %s (use --force to override)", ErrSymlinkConflict, link)
return res
}
if !opts.DryRun {
os.Remove(sc.Path)
}
}
res.Err = SyncTarget(t.Name, t.Target, opts.Source, opts.DryRun, opts.ProjectRoot)
}
return res
}
// SourceLinkWarnings returns one warning per first-level source link the walk
// did not follow. With pruneSkipped, a warning for an unavailable link also
// says that target entries were kept.
func SourceLinkWarnings(walk sourcewalk.Options, pruneSkipped bool) []string {
var warnings []string
for _, s := range walk.Follow.Skipped() {
w := s.String()
if pruneSkipped && s.Unavailable {
w += "; kept existing target entries, nothing pruned this run"
}
warnings = append(warnings, w)
}
return warnings
}
// addTargetWarnings records warnings about the target's own config.
func (res *SkillTargetResult) addTargetWarnings(warnings []string) {
for _, w := range warnings {
res.Warnings = append(res.Warnings, fmt.Sprintf("%s: %s", res.Name, w))
}
}
// addPrune records a prune's removals and reports its failure and warnings.
func (res *SkillTargetResult) addPrune(prune *PruneResult, err error) {
if err != nil {
res.Warnings = append(res.Warnings, fmt.Sprintf("%s: prune failed: %v", res.Name, err))
}
if prune != nil {
res.Pruned = prune.Removed
res.Warnings = append(res.Warnings, prune.Warnings...)
}
}