diff --git a/cmd/skillshare/sync.go b/cmd/skillshare/sync.go index 6f0f6f013..59dfe3f52 100644 --- a/cmd/skillshare/sync.go +++ b/cmd/skillshare/sync.go @@ -102,8 +102,16 @@ func cmdSync(args []string) error { } if mode == modeProject { - if hasAll && !jsonOutput { - ui.Warning("--all is not supported in project mode (extras are global only)") + if hasAll { + // Run project extras sync after project skills sync + defer func() { + fmt.Println() + if extrasErr := cmdSyncExtras(append([]string{"-p"}, rest...)); extrasErr != nil { + if !jsonOutput { + ui.Warning("Extras sync: %v", extrasErr) + } + } + }() } stats, results, err := cmdSyncProject(cwd, dryRun, force, jsonOutput) stats.ProjectScope = true diff --git a/cmd/skillshare/sync_extras.go b/cmd/skillshare/sync_extras.go index c03cb3fe7..b5d4ef761 100644 --- a/cmd/skillshare/sync_extras.go +++ b/cmd/skillshare/sync_extras.go @@ -16,8 +16,31 @@ import ( func cmdSyncExtras(args []string) error { start := time.Now() - dryRun, force, _ := parseSyncFlags(args) + mode, rest, err := parseModeArgs(args) + if err != nil { + return err + } + dryRun, force, _ := parseSyncFlags(rest) + + cwd, _ := os.Getwd() + if mode == modeAuto { + if projectConfigExists(cwd) { + mode = modeProject + } else { + mode = modeGlobal + } + } + + applyModeLabel(mode) + + if mode == modeProject { + return cmdSyncExtrasProject(cwd, dryRun, force, start) + } + return cmdSyncExtrasGlobal(dryRun, force, start) +} + +func cmdSyncExtrasGlobal(dryRun, force bool, start time.Time) error { cfg, err := config.Load() if err != nil { return err @@ -126,6 +149,103 @@ func cmdSyncExtras(args []string) error { return nil } +func cmdSyncExtrasProject(cwd string, dryRun, force bool, start time.Time) error { + projCfg, err := config.LoadProject(cwd) + if err != nil { + return err + } + + if len(projCfg.Extras) == 0 { + ui.Info("No extras configured in project.") + ui.Info("Run 'skillshare extras init --target -p' to add one.") + return nil + } + + if dryRun { + ui.Warning("Dry run mode - no changes will be made") + } + + var totalSynced, totalSkipped, totalPruned, totalErrors int + + for _, extra := range projCfg.Extras { + extraSource := config.ExtrasSourceDirProject(cwd, extra.Name) + + ui.Header(capitalize(extra.Name)) + + if _, statErr := os.Stat(extraSource); os.IsNotExist(statErr) { + ui.Info("Source directory does not exist: %s", extraSource) + ui.Info("Create it to start syncing %s", extra.Name) + continue + } + + for _, target := range extra.Targets { + mode := target.Mode + if mode == "" { + mode = "merge" + } + + // Resolve relative paths against project root + targetPath := target.Path + if !filepath.IsAbs(targetPath) { + targetPath = filepath.Join(cwd, targetPath) + } + + result, syncErr := sync.SyncExtra(extraSource, targetPath, mode, dryRun, force) + shortTarget := shortenPath(targetPath) + + if syncErr != nil { + ui.Warning(" %s: %v", shortTarget, syncErr) + totalErrors++ + continue + } + + totalSynced += result.Synced + totalSkipped += result.Skipped + totalPruned += result.Pruned + totalErrors += len(result.Errors) + + verb := syncVerb(mode) + if result.Synced > 0 { + parts := []string{fmt.Sprintf("%d files %s", result.Synced, verb)} + if result.Pruned > 0 { + parts = append(parts, fmt.Sprintf("%d pruned", result.Pruned)) + } + ui.Success(" %s %s (%s)", shortTarget, strings.Join(parts, ", "), mode) + } else if result.Skipped > 0 { + ui.Warning(" %s %d files skipped (use --force to override)", shortTarget, result.Skipped) + } else { + ui.Success(" %s up to date (%s)", shortTarget, mode) + } + + for _, e := range result.Errors { + ui.Warning(" %s", e) + } + } + } + + status := "ok" + if totalErrors > 0 { + status = "partial" + } + e := oplog.NewEntry("sync-extras", status, time.Since(start)) + e.Args = map[string]any{ + "extras_count": len(projCfg.Extras), + "synced": totalSynced, + "skipped": totalSkipped, + "pruned": totalPruned, + "errors": totalErrors, + "dry_run": dryRun, + "force": force, + "scope": "project", + } + oplog.WriteWithLimit(config.ProjectConfigPath(cwd), oplog.OpsFile, e, logMaxEntries()) //nolint:errcheck + + if totalErrors > 0 { + return fmt.Errorf("%d extras sync error(s)", totalErrors) + } + return nil +} + // syncVerb returns a user-facing verb for the given sync mode. func syncVerb(mode string) string { switch mode {