Files
skillshare/cmd/skillshare/sync.go
T
Willie 3bfad952e5 refactor(sync): remove symlink compat hint from sync output
Doctor is the right place for diagnostic hints. Sync runs frequently
and the repeated notice would be noisy.
2026-02-25 07:02:54 +08:00

345 lines
8.8 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"time"
"skillshare/internal/backup"
"skillshare/internal/config"
"skillshare/internal/oplog"
"skillshare/internal/sync"
"skillshare/internal/trash"
"skillshare/internal/ui"
"skillshare/internal/utils"
)
type syncLogStats struct {
Targets int
Failed int
DryRun bool
Force bool
ProjectScope bool
}
func cmdSync(args []string) error {
start := time.Now()
mode, rest, err := parseModeArgs(args)
if err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("cannot determine working directory: %w", err)
}
if mode == modeAuto {
if projectConfigExists(cwd) {
mode = modeProject
} else {
mode = modeGlobal
}
}
applyModeLabel(mode)
dryRun, force := parseSyncFlags(rest)
if mode == modeProject {
stats, err := cmdSyncProject(cwd, dryRun, force)
stats.ProjectScope = true
logSyncOp(config.ProjectConfigPath(cwd), stats, start, err)
return err
}
cfg, err := config.Load()
if err != nil {
return err
}
// Ensure source exists
if _, err := os.Stat(cfg.Source); os.IsNotExist(err) {
return fmt.Errorf("source directory does not exist: %s", cfg.Source)
}
// Backup targets before sync (only if not dry-run)
if !dryRun {
backupTargetsBeforeSync(cfg)
}
// Check for name collisions before syncing (per-target aware)
discoveredSkills, discoverErr := sync.DiscoverSourceSkills(cfg.Source)
if discoverErr == nil {
reportCollisions(discoveredSkills, cfg.Targets)
}
ui.Header("Syncing skills")
if dryRun {
ui.Warning("Dry run mode - no changes will be made")
}
failedTargets := 0
for name, target := range cfg.Targets {
if err := syncTarget(name, target, cfg, dryRun, force); err != nil {
ui.Error("%s: %v", name, err)
failedTargets++
}
}
var syncErr error
if failedTargets > 0 {
syncErr = fmt.Errorf("some targets failed to sync")
}
// Opportunistic cleanup of expired trash items
if !dryRun {
if n, _ := trash.Cleanup(trash.TrashDir(), 0); n > 0 {
ui.Info("Cleaned up %d expired trash item(s)", n)
}
}
logSyncOp(config.ConfigPath(), syncLogStats{
Targets: len(cfg.Targets),
Failed: failedTargets,
DryRun: dryRun,
Force: force,
}, start, syncErr)
return syncErr
}
func parseSyncFlags(args []string) (dryRun bool, force bool) {
for _, arg := range args {
switch arg {
case "--dry-run", "-n":
dryRun = true
case "--force", "-f":
force = true
}
}
return dryRun, force
}
func logSyncOp(cfgPath string, stats syncLogStats, start time.Time, cmdErr error) {
e := oplog.NewEntry("sync", statusFromErr(cmdErr), time.Since(start))
e.Args = map[string]any{
"targets_total": stats.Targets,
"targets_failed": stats.Failed,
"dry_run": stats.DryRun,
"force": stats.Force,
"scope": "global",
}
if stats.ProjectScope {
e.Args["scope"] = "project"
}
if cmdErr != nil {
e.Message = cmdErr.Error()
}
oplog.Write(cfgPath, oplog.OpsFile, e) //nolint:errcheck
}
func backupTargetsBeforeSync(cfg *config.Config) {
backedUp := false
for name, target := range cfg.Targets {
backupPath, err := backup.Create(name, target.Path)
if err != nil {
ui.Warning("Failed to backup %s: %v", name, err)
} else if backupPath != "" {
if !backedUp {
ui.Header("Backing up")
backedUp = true
}
ui.Success("%s -> %s", name, backupPath)
}
}
}
func syncTarget(name string, target config.TargetConfig, cfg *config.Config, dryRun, force bool) error {
// Determine mode: target-specific > global > default
mode := target.Mode
if mode == "" {
mode = cfg.Mode
}
if mode == "" {
mode = "merge"
}
switch mode {
case "merge":
return syncMergeMode(name, target, cfg.Source, dryRun, force)
case "copy":
return syncCopyMode(name, target, cfg.Source, dryRun, force)
default:
return syncSymlinkMode(name, target, cfg.Source, dryRun, force)
}
}
func syncMergeMode(name string, target config.TargetConfig, source string, dryRun, force bool) error {
result, err := sync.SyncTargetMerge(name, target, source, dryRun, force)
if err != nil {
return err
}
// Prune orphan links (skills that no longer exist in source)
pruneResult, pruneErr := sync.PruneOrphanLinks(target.Path, source, target.Include, target.Exclude, name, dryRun, force)
if pruneErr != nil {
ui.Warning("%s: prune failed: %v", name, pruneErr)
}
// Report results
linkedCount := len(result.Linked)
updatedCount := len(result.Updated)
skippedCount := len(result.Skipped)
removedCount := 0
if pruneResult != nil {
removedCount = len(pruneResult.Removed)
skippedCount += len(pruneResult.LocalDirs)
}
if linkedCount > 0 || updatedCount > 0 || removedCount > 0 {
ui.Success("%s: merged (%d linked, %d local, %d updated, %d pruned)",
name, linkedCount, skippedCount, updatedCount, removedCount)
} else if skippedCount > 0 {
ui.Success("%s: merged (%d local skills preserved)", name, skippedCount)
} else {
ui.Success("%s: merged (no skills)", name)
}
// Show filter summary (omit empty, human-readable)
if len(target.Include) > 0 {
ui.Info(" include: %s", strings.Join(target.Include, ", "))
}
if len(target.Exclude) > 0 {
ui.Info(" exclude: %s", strings.Join(target.Exclude, ", "))
}
// Show prune warnings
if pruneResult != nil {
for _, warn := range pruneResult.Warnings {
ui.Warning(" %s", warn)
}
}
return nil
}
func syncCopyMode(name string, target config.TargetConfig, source string, dryRun, force bool) error {
result, err := sync.SyncTargetCopy(name, target, source, dryRun, force)
if err != nil {
return err
}
// Prune orphan copies
pruneResult, pruneErr := sync.PruneOrphanCopies(target.Path, source, target.Include, target.Exclude, name, dryRun)
if pruneErr != nil {
ui.Warning("%s: prune failed: %v", name, pruneErr)
}
// Report results
copiedCount := len(result.Copied)
updatedCount := len(result.Updated)
skippedCount := len(result.Skipped)
removedCount := 0
if pruneResult != nil {
removedCount = len(pruneResult.Removed)
}
if copiedCount > 0 || updatedCount > 0 || removedCount > 0 {
ui.Success("%s: copied (%d new, %d skipped, %d updated, %d pruned)",
name, copiedCount, skippedCount, updatedCount, removedCount)
} else if skippedCount > 0 {
ui.Success("%s: copied (%d skipped, up to date)", name, skippedCount)
} else {
ui.Success("%s: copied (no skills)", name)
}
// Show filter summary
if len(target.Include) > 0 {
ui.Info(" include: %s", strings.Join(target.Include, ", "))
}
if len(target.Exclude) > 0 {
ui.Info(" exclude: %s", strings.Join(target.Exclude, ", "))
}
// Show prune warnings
if pruneResult != nil {
for _, warn := range pruneResult.Warnings {
ui.Warning(" %s", warn)
}
}
return nil
}
func reportCollisions(skills []sync.DiscoveredSkill, targets map[string]config.TargetConfig) {
global, perTarget := sync.CheckNameCollisionsForTargets(skills, targets)
if len(global) == 0 {
return
}
if len(perTarget) > 0 {
// Real per-target collisions — actionable warning
ui.Header("Name conflicts detected")
for _, tc := range perTarget {
for _, c := range tc.Collisions {
ui.Warning("Target '%s': skill name '%s' is defined in multiple places:", tc.TargetName, c.Name)
for _, p := range c.Paths {
ui.Info(" - %s", p)
}
}
}
ui.Info("Rename one in SKILL.md or adjust include/exclude filters")
fmt.Println()
} else {
// Global collision exists but filters isolate them — informational
ui.Info("Duplicate skill names exist but are isolated by target filters:")
for _, c := range global {
ui.Info(" '%s' (%d definitions)", c.Name, len(c.Paths))
}
fmt.Println()
}
}
func syncSymlinkMode(name string, target config.TargetConfig, source string, dryRun, force bool) error {
status := sync.CheckStatus(target.Path, source)
// Handle conflicts
if status == sync.StatusConflict && !force {
link, err := utils.ResolveLinkTarget(target.Path)
if err != nil {
link = "(unable to resolve target)"
}
return fmt.Errorf("conflict - symlink points to %s (use --force to override)", link)
}
if status == sync.StatusConflict && force {
if !dryRun {
os.Remove(target.Path)
}
}
if err := sync.SyncTarget(name, target, source, dryRun); err != nil {
return err
}
switch status {
case sync.StatusLinked:
ui.Success("%s: already linked", name)
case sync.StatusNotExist:
ui.Success("%s: symlink created", name)
ui.Warning(" Symlink mode: deleting files in %s will delete from source!", target.Path)
ui.Info(" Use 'skillshare target remove %s' to safely unlink", name)
case sync.StatusHasFiles:
ui.Success("%s: files migrated and linked", name)
ui.Warning(" Symlink mode: deleting files in %s will delete from source!", target.Path)
ui.Info(" Use 'skillshare target remove %s' to safely unlink", name)
case sync.StatusBroken:
ui.Success("%s: broken link fixed", name)
case sync.StatusConflict:
ui.Success("%s: conflict resolved (forced)", name)
}
return nil
}