Files
skillshare/internal/sync/skill_run.go
T
Willie 80ac109f72 fix(sync): show unmatched include warnings in every sync view (#328)
The unmatched include warning from #326 was written straight to
DiagOutput. The dashboard sync never showed it because DiagOutput is the
server's stdout, and an interactive CLI sync could redraw its progress
area over it. Return the warnings with the merge and copy results so
SyncSkillTarget adds them to the target's warnings, which both the CLI
and the API already report. Prefix them with the target name, since the
API returns one list for all targets.

A top-level skill name has no path separator, so the suggestion cut off
its first character and suggested "alpha" for a typo like "lpha". Compare
the whole name in that case.

Refs #325
2026-10-02 16:24:03 +08:00

125 lines
4.2 KiB
Go

package sync
import (
"errors"
"fmt"
"os"
"skillshare/internal/config"
"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
// 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)
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)
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
}
// 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...)
}
}