Files
skillshare/cmd/skillshare/sync_project.go
T
Willie 41dd81b7f2 refactor: migrate all callers to SkillsConfig() / EnsureSkills()
44 files across CLI commands, sync engine, and server handlers now use
SkillsConfig() for reads and EnsureSkills() for writes instead of
accessing flat TargetConfig fields directly.

Key changes:
- sync engine (SyncTargetMerge/Copy/Symlink): reads sc := target.SkillsConfig()
- target commands: filter add/remove and mode set use EnsureSkills()
- target TUI: mode picker, filter ops, detail panel all via SkillsConfig()
- server handlers: backup, restore, targets, sync, diff, collect, analyze
- init/init_project: new target creation uses Skills sub-key
- doctor, status, diff: target display reads from SkillsConfig()
- computeTargetDiff: accepts config.TargetConfig directly (was anon struct)
2026-03-29 04:59:23 +08:00

154 lines
3.8 KiB
Go

package main
import (
"fmt"
"os"
"time"
"skillshare/internal/config"
"skillshare/internal/skillignore"
"skillshare/internal/sync"
"skillshare/internal/trash"
"skillshare/internal/ui"
)
func cmdSyncProject(root string, dryRun, force, jsonOutput bool) (syncLogStats, []syncTargetResult, *skillignore.IgnoreStats, error) {
start := time.Now()
stats := syncLogStats{
DryRun: dryRun,
Force: force,
ProjectScope: true,
}
if !projectConfigExists(root) {
if err := performProjectInit(root, projectInitOptions{}); err != nil {
return stats, nil, nil, err
}
}
runtime, err := loadProjectRuntime(root)
if err != nil {
return stats, nil, nil, err
}
stats.Targets = len(runtime.config.Targets)
// Validate project config before sync
warnings, validErr := config.ValidateProjectConfig(runtime.config, root)
if validErr != nil {
return stats, nil, nil, validErr
}
if !jsonOutput {
for _, w := range warnings {
ui.Warning("%s", w)
}
}
// ValidateProjectConfig warns on missing source (may not exist yet after init).
// Gate here as a hard error — sync cannot proceed without source skills.
if _, err := os.Stat(runtime.sourcePath); os.IsNotExist(err) {
return stats, nil, nil, fmt.Errorf("source directory does not exist: %s", runtime.sourcePath)
}
// Phase 1: Discovery
var spinner *ui.Spinner
if !jsonOutput {
spinner = ui.StartSpinner("Discovering skills")
}
discoveredSkills, ignoreStats, discoverErr := sync.DiscoverSourceSkillsWithStats(runtime.sourcePath)
if discoverErr != nil {
if spinner != nil {
spinner.Fail("Discovery failed")
}
return stats, nil, nil, discoverErr
}
if spinner != nil {
spinner.Success(fmt.Sprintf("Discovered %d skills", len(discoveredSkills)))
reportCollisions(discoveredSkills, runtime.targets)
}
// Phase 2: Per-target sync
if !jsonOutput {
ui.Header("Syncing skills (project)")
if dryRun {
ui.Warning("Dry run mode - no changes will be made")
}
}
var entries []syncTargetEntry
notFoundCount := 0
for _, entry := range runtime.config.Targets {
name := entry.Name
target, ok := runtime.targets[name]
if !ok {
if !jsonOutput {
ui.Error("%s: target not found", name)
}
notFoundCount++
continue
}
mode := target.SkillsConfig().Mode
if mode == "" {
mode = "merge"
}
entries = append(entries, syncTargetEntry{name: name, target: target, mode: mode})
}
var results []syncTargetResult
var failedTargets int
if jsonOutput {
results, failedTargets = runParallelSyncQuiet(entries, runtime.sourcePath, discoveredSkills, dryRun, force)
} else {
results, failedTargets = runParallelSync(entries, runtime.sourcePath, discoveredSkills, dryRun, force)
}
failedTargets += notFoundCount
var totals syncModeStats
for _, r := range results {
totals.linked += r.stats.linked
totals.local += r.stats.local
totals.updated += r.stats.updated
totals.pruned += r.stats.pruned
}
stats.Failed = failedTargets
if !jsonOutput {
// Phase 3: Summary
ui.SyncSummary(ui.SyncStats{
Targets: len(runtime.config.Targets),
Linked: totals.linked,
Local: totals.local,
Updated: totals.updated,
Pruned: totals.pruned,
Duration: time.Since(start),
})
// Show ignored skills from .skillignore
printIgnoredSkills(ignoreStats)
}
if failedTargets > 0 {
return stats, results, ignoreStats, fmt.Errorf("some targets failed to sync")
}
// Opportunistic cleanup of expired trash items
if !dryRun {
if n, _ := trash.Cleanup(trash.ProjectTrashDir(root), 0); n > 0 {
if !jsonOutput {
ui.Info("Cleaned up %d expired trash item(s)", n)
}
}
}
return stats, results, ignoreStats, nil
}
func projectTargetDisplayPath(entry config.ProjectTargetEntry) string {
if p := entry.SkillsConfig().Path; p != "" {
return p
}
if known, ok := config.LookupProjectTarget(entry.Name); ok {
return known.Path
}
return ""
}