mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Make the v0.19.16 sources map actually do something: route all path reads through Config.EffectiveSkillsSource() / EffectiveAgentsSource() / EffectiveExtrasSource() so users who set sources.skills / sources.agents / sources.extras get the configured paths everywhere. Mechanical changes (~50 files): - cmd/skillshare/**: replace cfg.Source / cfg.ExtrasSource reads with cfg.EffectiveSkillsSource() / cfg.EffectiveExtrasSource() - internal/server/**: replace s.cfg.Source / s.cfg.ExtrasSource reads with the same helpers; server.skillsSource() / gitignoreDir() use the global-mode helper - internal/config/config_validate.go: validate using EffectiveSkillsSource while still rejecting configs that set neither field explicitly - internal/config/reconcile.go, registry.go: read via helpers Behavioral fixes (paired with the migration): - extras init / extras source / POST /api/extras no longer silently backfill the legacy extras_source field — the runtime fallback inside EffectiveExtrasSource() handles it. This stops surprise diffs in users' config.yaml after running extras commands. - extras source command writes back to whichever field the user already configured (sources.extras if set, otherwise legacy extras_source) so the update is not shadowed. - source_type in /api/extras and extras list correctly reports "default" vs "extras_source" by inspecting the raw (unresolved) configured value; sources.extras counts as configured for this purpose. UI: - ui/src/lib/fieldDocs.ts: neutralize sources / sources.* descriptions so they describe both project and global modes (Phase 5). Preserved (intentional): - Direct writes to cfg.Source / cfg.AgentsSource / cfg.ExtrasSource in init, migrate.go, cloneForSave, and server.reloadConfig — these are raw-field writes, not path reads. - Updated test expectations for the removed backfills.
180 lines
4.2 KiB
Go
180 lines
4.2 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"skillshare/internal/install"
|
|
)
|
|
|
|
// handleUpdateStream serves an SSE endpoint that streams update progress in real time.
|
|
// Events:
|
|
// - "start" → {"total": N} after collecting updatable items
|
|
// - "result" → updateResultItem after each item is updated
|
|
// - "done" → {"results":…,"summary":…} final payload
|
|
//
|
|
// Query params: names (comma-separated), force, skipAudit
|
|
func (s *Server) handleUpdateStream(w http.ResponseWriter, r *http.Request) {
|
|
safeSend, ok := initSSE(w)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
start := time.Now()
|
|
ctx := r.Context()
|
|
|
|
force := r.URL.Query().Get("force") == "true"
|
|
skipAudit := r.URL.Query().Get("skipAudit") == "true"
|
|
|
|
// Snapshot source under RLock, then release before slow I/O.
|
|
s.mu.RLock()
|
|
source := s.cfg.EffectiveSkillsSource()
|
|
s.mu.RUnlock()
|
|
|
|
// Collect items to update based on "names" query param.
|
|
// If names is empty, update all updatable items.
|
|
namesParam := r.URL.Query().Get("names")
|
|
|
|
type updateItem struct {
|
|
name string
|
|
isRepo bool
|
|
path string
|
|
}
|
|
|
|
var items []updateItem
|
|
|
|
if namesParam != "" {
|
|
// Update specific items by name
|
|
for name := range strings.SplitSeq(namesParam, ",") {
|
|
name = strings.TrimSpace(name)
|
|
if name == "" {
|
|
continue
|
|
}
|
|
// Check if it's a tracked repo
|
|
repoName := name
|
|
if !strings.HasPrefix(repoName, "_") {
|
|
repoName = "_" + name
|
|
}
|
|
repoPath := filepath.Join(source, repoName)
|
|
if install.IsGitRepo(repoPath) {
|
|
items = append(items, updateItem{name: repoName, isRepo: true, path: repoPath})
|
|
continue
|
|
}
|
|
// Check original name as repo
|
|
origPath := filepath.Join(source, name)
|
|
if install.IsGitRepo(origPath) {
|
|
items = append(items, updateItem{name: name, isRepo: true, path: origPath})
|
|
continue
|
|
}
|
|
// Regular skill
|
|
skillPath := filepath.Join(source, name)
|
|
items = append(items, updateItem{name: name, isRepo: false, path: skillPath})
|
|
}
|
|
} else {
|
|
// Update all: tracked repos + regular skills
|
|
repos, err := install.GetTrackedRepos(source)
|
|
if err == nil {
|
|
for _, repo := range repos {
|
|
items = append(items, updateItem{
|
|
name: repo,
|
|
isRepo: true,
|
|
path: filepath.Join(source, repo),
|
|
})
|
|
}
|
|
}
|
|
skills, err := getServerUpdatableSkills(source, s.skillsStore)
|
|
if err == nil {
|
|
for _, skill := range skills {
|
|
items = append(items, updateItem{
|
|
name: skill,
|
|
isRepo: false,
|
|
path: filepath.Join(source, skill),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
safeSend("start", map[string]int{"total": len(items)})
|
|
|
|
var results []updateResultItem
|
|
summary := struct {
|
|
Updated int `json:"updated"`
|
|
UpToDate int `json:"upToDate"`
|
|
Blocked int `json:"blocked"`
|
|
Errors int `json:"errors"`
|
|
Skipped int `json:"skipped"`
|
|
}{}
|
|
|
|
for _, item := range items {
|
|
// Check if client disconnected
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
default:
|
|
}
|
|
|
|
var result updateResultItem
|
|
|
|
// Lock per-item for write operations
|
|
s.mu.Lock()
|
|
if item.isRepo {
|
|
result = s.updateTrackedRepo(item.name, item.path, force, skipAudit)
|
|
} else {
|
|
result = s.updateRegularSkill(item.name, item.path, skipAudit)
|
|
}
|
|
s.mu.Unlock()
|
|
|
|
results = append(results, result)
|
|
|
|
switch result.Action {
|
|
case "updated":
|
|
summary.Updated++
|
|
case "up-to-date":
|
|
summary.UpToDate++
|
|
case "blocked":
|
|
summary.Blocked++
|
|
case "error":
|
|
summary.Errors++
|
|
case "skipped":
|
|
summary.Skipped++
|
|
}
|
|
|
|
safeSend("result", result)
|
|
}
|
|
|
|
// Write ops log
|
|
s.mu.Lock()
|
|
status := "ok"
|
|
msg := ""
|
|
if summary.Blocked > 0 {
|
|
status = "partial"
|
|
msg = fmt.Sprintf("%d update(s) blocked by security audit", summary.Blocked)
|
|
}
|
|
if summary.Errors > 0 {
|
|
status = "partial"
|
|
if msg != "" {
|
|
msg += fmt.Sprintf(", %d update(s) failed", summary.Errors)
|
|
} else {
|
|
msg = fmt.Sprintf("%d update(s) failed", summary.Errors)
|
|
}
|
|
}
|
|
s.writeOpsLog("update", status, start, map[string]any{
|
|
"name": "--all-stream",
|
|
"force": force,
|
|
"skip_audit": skipAudit,
|
|
"results_total": len(results),
|
|
"results_failed": summary.Errors,
|
|
"results_blocked": summary.Blocked,
|
|
"scope": "ui",
|
|
}, msg)
|
|
s.mu.Unlock()
|
|
|
|
safeSend("done", map[string]any{
|
|
"results": results,
|
|
"summary": summary,
|
|
})
|
|
}
|