Files
skillshare/internal/server/handler_overview.go
T
Willie 996d00e342 feat(config): migrate consumers to Effective*Source helpers (Phase 3+5)
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.
2026-05-21 14:49:11 +08:00

124 lines
2.8 KiB
Go

package server
import (
"net/http"
"os"
"path/filepath"
"strings"
"skillshare/internal/git"
"skillshare/internal/install"
"skillshare/internal/resource"
"skillshare/internal/sync"
"skillshare/internal/utils"
versioncheck "skillshare/internal/version"
)
type trackedRepoItem struct {
Name string `json:"name"`
SkillCount int `json:"skillCount"`
Dirty bool `json:"dirty"`
}
func (s *Server) handleOverview(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
agentsSource := s.agentsSource()
extrasSource := s.cfg.EffectiveExtrasSource()
if s.IsProjectMode() {
extrasSource = filepath.Join(s.projectRoot, ".skillshare", "extras")
}
cfgMode := s.cfg.Mode
targetCount := len(s.cfg.Targets)
projectRoot := s.projectRoot
s.mu.RUnlock()
isProjectMode := projectRoot != ""
// Count skills
skills, err := sync.DiscoverSourceSkills(source)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
// Count top-level source entries (for display)
topLevelCount := 0
entries, _ := os.ReadDir(source)
for _, e := range entries {
if e.IsDir() && !utils.IsHidden(e.Name()) {
topLevelCount++
}
}
mode := cfgMode
if mode == "" {
mode = "merge"
}
// Tracked repos
trackedRepos := buildTrackedRepos(source, skills)
// Count agents
agentCount := 0
if agentsSource != "" {
if agents, discoverErr := (resource.AgentKind{}).Discover(agentsSource); discoverErr == nil {
agentCount = len(agents)
}
}
resp := map[string]any{
"source": source,
"skillCount": len(skills),
"agentCount": agentCount,
"topLevelCount": topLevelCount,
"targetCount": targetCount,
"mode": mode,
"version": versioncheck.Version,
"trackedRepos": trackedRepos,
"isProjectMode": isProjectMode,
}
if agentsSource != "" {
resp["agentsSource"] = agentsSource
}
if extrasSource != "" {
resp["extrasSource"] = extrasSource
}
if isProjectMode {
resp["projectRoot"] = projectRoot
}
writeJSON(w, resp)
}
func buildTrackedRepos(sourceDir string, skills []sync.DiscoveredSkill) []trackedRepoItem {
repoNames, err := install.GetTrackedRepos(sourceDir)
if err != nil || len(repoNames) == 0 {
return []trackedRepoItem{}
}
items := make([]trackedRepoItem, 0, len(repoNames))
for _, repoName := range repoNames {
repoPath := filepath.Join(sourceDir, repoName)
// Count skills belonging to this repo
skillCount := 0
for _, sk := range skills {
if sk.IsInRepo && strings.HasPrefix(sk.RelPath, repoName+"/") {
skillCount++
}
}
// Check git dirty status
dirty, _ := git.IsDirty(repoPath)
items = append(items, trackedRepoItem{
Name: repoName,
SkillCount: skillCount,
Dirty: dirty,
})
}
return items
}