diff --git a/cmd/skillshare/update.go b/cmd/skillshare/update.go index bf04731b3..4a593af0e 100644 --- a/cmd/skillshare/update.go +++ b/cmd/skillshare/update.go @@ -7,7 +7,6 @@ import ( "strings" "time" - "skillshare/internal/audit" "skillshare/internal/config" "skillshare/internal/install" "skillshare/internal/oplog" @@ -257,9 +256,9 @@ func cmdUpdate(args []string) error { t := targets[0] var updateErr error if t.isRepo { - updateErr = updateTrackedRepo(cfg, t.name, opts.dryRun, opts.force, opts.skipAudit, opts.diff, opts.threshold) + updateErr = updateTrackedRepo(cfg.Source, t.name, opts.dryRun, opts.force, opts.skipAudit, opts.diff, opts.threshold, "") } else { - updateErr = updateRegularSkill(cfg, t.name, opts.dryRun, opts.force, opts.skipAudit, opts.diff, opts.threshold, opts.auditVerbose) + updateErr = updateRegularSkill(cfg.Source, t.name, opts.dryRun, opts.force, opts.skipAudit, opts.diff, opts.threshold, opts.auditVerbose, "") } var opNames []string @@ -272,7 +271,7 @@ func cmdUpdate(args []string) error { return updateErr } - // Multiple targets: batch path with progress bar + // Multiple targets: batch path total := len(targets) ui.Header(fmt.Sprintf("Updating %d skill(s)", total)) fmt.Println() @@ -281,162 +280,8 @@ func cmdUpdate(args []string) error { ui.Warning("[dry-run] No changes will be made") } - progressBar := ui.StartProgress("Updating skills", total) - - var result updateResult - var auditEntries []batchAuditEntry - var blockedEntries []batchBlockedEntry - - // Group skills by RepoURL to optimize updates - repoGroups := make(map[string][]updateTarget) - var standaloneSkills []updateTarget - var trackedRepos []updateTarget - - for _, t := range targets { - if t.isRepo { - trackedRepos = append(trackedRepos, t) - continue - } - - meta, err := install.ReadMeta(t.path) - if err == nil && meta != nil && meta.RepoURL != "" { - repoGroups[meta.RepoURL] = append(repoGroups[meta.RepoURL], t) - } else { - standaloneSkills = append(standaloneSkills, t) - } - } - - // 1. Process tracked repositories (git pull) - for _, t := range trackedRepos { - progressBar.UpdateTitle(fmt.Sprintf("Updating %s", t.name)) - updated, auditResult, err := updateTrackedRepoQuick(t.name, t.path, opts.dryRun, opts.force, opts.skipAudit, opts.threshold, audit.ScanSkill) - if err != nil { - if isSecurityError(err) { - result.securityFailed++ - blockedEntries = append(blockedEntries, batchBlockedEntry{name: t.name, errMsg: err.Error()}) - } else { - result.skipped++ - } - } else if updated { - result.updated++ - } else { - result.skipped++ - } - if auditResult != nil { - auditEntries = append(auditEntries, batchAuditEntryFromAuditResult(t.name, auditResult, opts.skipAudit)) - } - progressBar.Increment() - } - - // 2. Process grouped skills (one clone per repo) - for repoURL, groupTargets := range repoGroups { - if opts.dryRun { - for _, t := range groupTargets { - progressBar.UpdateTitle(fmt.Sprintf("Updating %s", t.name)) - progressBar.Increment() - result.skipped++ - } - continue - } - - progressBar.UpdateTitle(fmt.Sprintf("Updating %d skills from %s", len(groupTargets), repoURL)) - - // Map repo-internal subdir → local absolute path - skillTargetMap := make(map[string]string) - pathToTarget := make(map[string]updateTarget) - for _, t := range groupTargets { - meta, _ := install.ReadMeta(t.path) - if meta != nil { - skillTargetMap[meta.Subdir] = t.path - pathToTarget[meta.Subdir] = t - } - } - - batchOpts := install.InstallOptions{ - Force: true, - Update: true, - SkipAudit: opts.skipAudit, - AuditThreshold: opts.threshold, - } - if ui.IsTTY() { - batchOpts.OnProgress = func(line string) { - progressBar.UpdateTitle(line) - } - } - - batchResult, err := install.UpdateSkillsFromRepo(repoURL, skillTargetMap, batchOpts) - if err != nil { - for _, t := range groupTargets { - progressBar.UpdateTitle(fmt.Sprintf("Failed %s: %v", t.name, err)) - result.skipped++ - progressBar.Increment() - } - continue - } - - for subdir := range skillTargetMap { - t := pathToTarget[subdir] - progressBar.UpdateTitle(fmt.Sprintf("Updating %s", t.name)) - - if ui.IsTTY() { - time.Sleep(50 * time.Millisecond) - } - - if err := batchResult.Errors[subdir]; err != nil { - if isSecurityError(err) { - result.securityFailed++ - blockedEntries = append(blockedEntries, batchBlockedEntry{name: t.name, errMsg: err.Error()}) - } else { - result.skipped++ - } - } else if res := batchResult.Results[subdir]; res != nil { - result.updated++ - auditEntries = append(auditEntries, batchAuditEntryFromInstallResult(t.name, res)) - } else { - result.skipped++ - } - progressBar.Increment() - } - } - - // 3. Process standalone skills - for _, t := range standaloneSkills { - progressBar.UpdateTitle(fmt.Sprintf("Updating %s", t.name)) - installOpts := install.InstallOptions{ - Force: true, Update: true, - SkipAudit: opts.skipAudit, AuditThreshold: opts.threshold, - } - updated, installRes, err := updateSkillFromMeta(t.path, opts.dryRun, installOpts) - if err != nil && isSecurityError(err) { - result.securityFailed++ - blockedEntries = append(blockedEntries, batchBlockedEntry{name: t.name, errMsg: err.Error()}) - } else if updated { - result.updated++ - } else { - result.skipped++ - } - if installRes != nil { - auditEntries = append(auditEntries, batchAuditEntryFromInstallResult(t.name, installRes)) - } - progressBar.Increment() - } - - progressBar.Stop() - - if !opts.dryRun { - displayUpdateBlockedSection(blockedEntries) - displayUpdateAuditResults(auditEntries, opts.auditVerbose) - fmt.Println() - ui.SuccessMsg("Updated %d, skipped %d of %d skill(s)", result.updated, result.skipped, total) - if result.securityFailed > 0 { - ui.Warning("Blocked: %d (security)", result.securityFailed) - } - } - - if result.updated > 0 && !opts.dryRun { - ui.SectionLabel("Next Steps") - ui.Info("Run 'skillshare sync' to distribute changes") - } + uc := &updateContext{sourcePath: cfg.Source, opts: opts} + _, batchErr := executeBatchUpdate(uc, targets) // Build oplog names var opNames []string @@ -448,11 +293,6 @@ func cmdUpdate(args []string) error { opNames = append(opNames, "--group="+g) } } - - var batchErr error - if result.securityFailed > 0 { - batchErr = fmt.Errorf("%d repo(s) blocked by security audit", result.securityFailed) - } logUpdateOp(config.ConfigPath(), opNames, start, batchErr) return batchErr diff --git a/cmd/skillshare/update_handlers.go b/cmd/skillshare/update_handlers.go index 3868e1a4b..9b6809d07 100644 --- a/cmd/skillshare/update_handlers.go +++ b/cmd/skillshare/update_handlers.go @@ -11,7 +11,6 @@ import ( "time" "skillshare/internal/audit" - "skillshare/internal/config" "skillshare/internal/git" "skillshare/internal/install" "skillshare/internal/ui" @@ -90,8 +89,8 @@ func auditGateAfterPull(repoPath, beforeHash string, skipAudit bool, threshold s return result, fmt.Errorf("security audit failed — findings at/above %s detected — rolled back (use --skip-audit to bypass): %w", normalizedThreshold, audit.ErrBlocked) } -func updateTrackedRepo(cfg *config.Config, repoName string, dryRun, force, skipAudit, showDiff bool, threshold string) error { - repoPath := filepath.Join(cfg.Source, repoName) +func updateTrackedRepo(sourcePath, repoName string, dryRun, force, skipAudit, showDiff bool, threshold, projectRoot string) error { + repoPath := filepath.Join(sourcePath, repoName) start := time.Now() ui.StepStart("Repo", repoName) @@ -195,7 +194,13 @@ func updateTrackedRepo(cfg *config.Config, repoName string, dryRun, force, skipA fmt.Println() // Post-pull audit gate - if _, err := auditGateAfterPull(repoPath, info.BeforeHash, skipAudit, threshold, audit.ScanSkill); err != nil { + var scanFn auditScanFunc = audit.ScanSkill + if projectRoot != "" { + scanFn = func(path string) (*audit.Result, error) { + return audit.ScanSkillForProject(path, projectRoot) + } + } + if _, err := auditGateAfterPull(repoPath, info.BeforeHash, skipAudit, threshold, scanFn); err != nil { return err } @@ -208,8 +213,8 @@ func updateTrackedRepo(cfg *config.Config, repoName string, dryRun, force, skipA return nil } -func updateRegularSkill(cfg *config.Config, skillName string, dryRun, force, skipAudit, showDiff bool, threshold string, auditVerbose bool) error { - skillPath := filepath.Join(cfg.Source, skillName) +func updateRegularSkill(sourcePath, skillName string, dryRun, force, skipAudit, showDiff bool, threshold string, auditVerbose bool, projectRoot string) error { + skillPath := filepath.Join(sourcePath, skillName) // Read metadata to get source meta, err := install.ReadMeta(skillPath) @@ -244,10 +249,11 @@ func updateRegularSkill(cfg *config.Config, skillName string, dryRun, force, ski spinner := ui.StartSpinner("Updating...") opts := install.InstallOptions{ - Force: true, - Update: true, - SkipAudit: skipAudit, - AuditThreshold: threshold, + Force: true, + Update: true, + SkipAudit: skipAudit, + AuditThreshold: threshold, + AuditProjectRoot: projectRoot, } if ui.IsTTY() { opts.OnProgress = func(line string) { @@ -355,35 +361,35 @@ func updateSkillFromMeta(skillPath string, dryRun bool, installOpts install.Inst // updateSkillOrRepo updates a skill or repo by name, handling _ prefix and // basename resolution. -func updateSkillOrRepo(cfg *config.Config, name string, dryRun, force, skipAudit, showDiff bool, threshold string, auditVerbose bool) error { +func updateSkillOrRepo(sourcePath, name string, dryRun, force, skipAudit, showDiff bool, threshold string, auditVerbose bool, projectRoot string) error { // Try tracked repo first (with _ prefix) repoName := name if !strings.HasPrefix(repoName, "_") { repoName = "_" + name } - repoPath := filepath.Join(cfg.Source, repoName) + repoPath := filepath.Join(sourcePath, repoName) if install.IsGitRepo(repoPath) { - return updateTrackedRepo(cfg, repoName, dryRun, force, skipAudit, showDiff, threshold) + return updateTrackedRepo(sourcePath, repoName, dryRun, force, skipAudit, showDiff, threshold, projectRoot) } // Try as regular skill (exact path) - skillPath := filepath.Join(cfg.Source, name) + skillPath := filepath.Join(sourcePath, name) if meta, err := install.ReadMeta(skillPath); err == nil && meta != nil { - return updateRegularSkill(cfg, name, dryRun, force, skipAudit, showDiff, threshold, auditVerbose) + return updateRegularSkill(sourcePath, name, dryRun, force, skipAudit, showDiff, threshold, auditVerbose, projectRoot) } // Check if it's a nested path that exists as git repo if install.IsGitRepo(skillPath) { - return updateTrackedRepo(cfg, name, dryRun, force, skipAudit, showDiff, threshold) + return updateTrackedRepo(sourcePath, name, dryRun, force, skipAudit, showDiff, threshold, projectRoot) } // Fallback: search by basename in nested skills and repos - if match, err := resolveByBasename(cfg.Source, name); err == nil { + if match, err := resolveByBasename(sourcePath, name); err == nil { if match.isRepo { - return updateTrackedRepo(cfg, match.name, dryRun, force, skipAudit, showDiff, threshold) + return updateTrackedRepo(sourcePath, match.name, dryRun, force, skipAudit, showDiff, threshold, projectRoot) } - return updateRegularSkill(cfg, match.name, dryRun, force, skipAudit, showDiff, threshold, auditVerbose) + return updateRegularSkill(sourcePath, match.name, dryRun, force, skipAudit, showDiff, threshold, auditVerbose, projectRoot) } else { return err } diff --git a/cmd/skillshare/update_project.go b/cmd/skillshare/update_project.go index ef828bd34..134594650 100644 --- a/cmd/skillshare/update_project.go +++ b/cmd/skillshare/update_project.go @@ -5,10 +5,7 @@ import ( "os" "path/filepath" "strings" - "time" - "skillshare/internal/audit" - "skillshare/internal/git" "skillshare/internal/install" "skillshare/internal/ui" "skillshare/internal/utils" @@ -155,9 +152,9 @@ func cmdUpdateProjectBatch(sourcePath string, opts *updateOptions, projectRoot s if len(targets) == 1 { t := targets[0] if t.isRepo { - return updateProjectTrackedRepo(t.name, t.path, opts.dryRun, opts.force, opts.skipAudit, opts.diff, opts.threshold, projectRoot) + return updateTrackedRepo(sourcePath, t.name, opts.dryRun, opts.force, opts.skipAudit, opts.diff, opts.threshold, projectRoot) } - return updateSingleProjectSkill(sourcePath, t.name, opts.dryRun, opts.force, opts.skipAudit, opts.diff, opts.threshold, opts.auditVerbose, projectRoot) + return updateRegularSkill(sourcePath, t.name, opts.dryRun, opts.force, opts.skipAudit, opts.diff, opts.threshold, opts.auditVerbose, projectRoot) } // Batch mode @@ -174,154 +171,6 @@ func cmdUpdateProjectBatch(sourcePath string, opts *updateOptions, projectRoot s return batchErr } -func updateSingleProjectSkill(sourcePath, name string, dryRun, force, skipAudit, showDiff bool, threshold string, auditVerbose bool, projectRoot string) error { - // Normalize _ prefix for tracked repos - repoName := name - if !strings.HasPrefix(repoName, "_") { - prefixed := filepath.Join(sourcePath, "_"+name) - if install.IsGitRepo(prefixed) { - repoName = "_" + name - } - } - repoPath := filepath.Join(sourcePath, repoName) - - // Try as tracked repo first - if install.IsGitRepo(repoPath) { - return updateProjectTrackedRepo(repoName, repoPath, dryRun, force, skipAudit, showDiff, threshold, projectRoot) - } - - // Regular skill with metadata - skillPath := filepath.Join(sourcePath, name) - if _, err := os.Stat(skillPath); err != nil { - return fmt.Errorf("skill '%s' not found", name) - } - - meta, err := install.ReadMeta(skillPath) - if err != nil || meta == nil { - return fmt.Errorf("%s is a local skill, nothing to update", name) - } - - source, err := install.ParseSource(meta.Source) - if err != nil { - return fmt.Errorf("invalid source for %s: %w", name, err) - } - - if dryRun { - ui.Info("[dry-run] would update %s", name) - return nil - } - - ui.StepStart("Skill", name) - ui.StepContinue("Source", meta.Source) - - // Snapshot before update for --diff - var beforeHashes map[string]string - if showDiff { - beforeHashes, _ = install.ComputeFileHashes(skillPath) - } - - startUpdate := time.Now() - spinner := ui.StartSpinner("Updating...") - opts := install.InstallOptions{ - Force: true, - Update: true, - SkipAudit: skipAudit, - AuditThreshold: threshold, - } - if ui.IsTTY() { - opts.OnProgress = func(line string) { - spinner.Update(" " + line) // Indent to align with tree - } - } - result, err := install.Install(source, skillPath, opts) - if err != nil { - spinner.Stop() - ui.StepResult("error", fmt.Sprintf("Failed: %v", err), 0) - return err - } - spinner.Stop() - ui.StepResult("success", "Updated successfully", time.Since(startUpdate)) - fmt.Println() - - renderInstallWarningsWithResult("", result.Warnings, auditVerbose, result) - - if showDiff { - afterHashes, _ := install.ComputeFileHashes(skillPath) - renderHashDiffSummary(beforeHashes, afterHashes) - } - - ui.SectionLabel("Next Steps") - ui.Info("Run 'skillshare sync' to distribute changes") - return nil -} - -// updateProjectTrackedRepo updates a single tracked repo in project mode (verbose). -func updateProjectTrackedRepo(repoName, repoPath string, dryRun, force, skipAudit, showDiff bool, threshold, projectRoot string) error { - if isDirty, _ := git.IsDirty(repoPath); isDirty { - if !force { - ui.Warning("%s has uncommitted changes (use --force to discard)", repoName) - return fmt.Errorf("uncommitted changes in %s", repoName) - } - if !dryRun { - if err := git.Restore(repoPath); err != nil { - return fmt.Errorf("failed to discard changes: %w", err) - } - } - } - - if dryRun { - ui.Info("[dry-run] would git pull %s", repoName) - return nil - } - - ui.StepStart("Repo", repoName) - - startUpdate := time.Now() - spinner := ui.StartSpinner("Checking status...") - var onProgress func(string) - if ui.IsTTY() { - onProgress = func(line string) { spinner.Update(line) } - } - - var info *git.UpdateInfo - var err error - if force { - info, err = git.ForcePullWithProgress(repoPath, git.AuthEnvForRepo(repoPath), onProgress) - } else { - info, err = git.PullWithProgress(repoPath, git.AuthEnvForRepo(repoPath), onProgress) - } - if err != nil { - spinner.Stop() - ui.StepResult("error", fmt.Sprintf("Failed: %v", err), 0) - return nil - } - - if info.UpToDate { - spinner.Stop() - ui.StepResult("success", "Already up to date", time.Since(startUpdate)) - return nil - } - - spinner.Stop() - ui.StepResult("success", fmt.Sprintf("%d commits, %d files updated", len(info.Commits), info.Stats.FilesChanged), time.Since(startUpdate)) - fmt.Println() - - if showDiff { - renderDiffSummary(repoPath, info.BeforeHash, info.AfterHash) - } - - scanFn := func(path string) (*audit.Result, error) { - return audit.ScanSkillForProject(path, projectRoot) - } - if _, err := auditGateAfterPull(repoPath, info.BeforeHash, skipAudit, threshold, scanFn); err != nil { - return err - } - - ui.SectionLabel("Next Steps") - ui.Info("Run 'skillshare sync' to distribute changes") - return nil -} - func updateAllProjectSkills(sourcePath string, dryRun, force, skipAudit, showDiff bool, threshold string, auditVerbose bool, projectRoot string) error { var targets []updateTarget @@ -379,9 +228,9 @@ func updateAllProjectSkills(sourcePath string, dryRun, force, skipAudit, showDif if total == 1 { t := targets[0] if t.isRepo { - return updateProjectTrackedRepo(t.name, t.path, dryRun, force, skipAudit, showDiff, threshold, projectRoot) + return updateTrackedRepo(sourcePath, t.name, dryRun, force, skipAudit, showDiff, threshold, projectRoot) } - return updateSingleProjectSkill(sourcePath, t.name, dryRun, force, skipAudit, showDiff, threshold, auditVerbose, projectRoot) + return updateRegularSkill(sourcePath, t.name, dryRun, force, skipAudit, showDiff, threshold, auditVerbose, projectRoot) } uc := &updateContext{