Files
skillshare/internal/server/handler_install_config.go
T
Willie a686ee910f fix(server): install config entries into the project source in project mode
The dashboard's install-from-config context read the global skills source
from s.cfg, which stays global in project mode. Missing entries were checked
against the global folder and installed there instead of the project's
.skillshare/skills. Use the project-aware skillsSource and reload the
metadata store from that same folder before reconciling.
2026-10-08 12:26:26 +08:00

129 lines
4.1 KiB
Go

package server
import (
"net/http"
"time"
"skillshare/internal/config"
"skillshare/internal/install"
"skillshare/internal/projectdir"
)
// serverInstallContext runs the CLI's bare `skillshare install` (install
// everything recorded in config that is not on disk) for the dashboard.
type serverInstallContext struct{ s *Server }
var _ install.InstallContext = serverInstallContext{}
// SourcePath is the project's skills folder in project mode, else the global one.
func (c serverInstallContext) SourcePath() string { return c.s.skillsSource() }
func (c serverInstallContext) ConfigSkills() []install.SkillEntryDTO {
if !c.s.IsProjectMode() {
return install.MetadataSkillEntries(install.LoadMetadataOrNew(c.SourcePath()))
}
return config.ProjectSkillEntries(c.s.projectCfg.Skills)
}
func (c serverInstallContext) Reconcile() error {
// Reload from the same folder the install wrote to; reloadSkillsStore reads the global one.
if st, err := install.LoadMetadataWithMigration(c.SourcePath(), ""); err == nil && st != nil {
c.s.skillsStore = st
}
c.s.reconcileSkillsConfig(c.SourcePath())
return nil
}
func (c serverInstallContext) PostInstallSkill(displayName string) error {
if !c.s.IsProjectMode() {
return nil
}
dir := c.s.gitignoreDir()
if dir == "" {
return nil
}
return install.UpdateGitIgnore(dir, c.s.projectGitignorePrefix()+"/"+displayName)
}
func (c serverInstallContext) Mode() string { return c.s.installLogMode() }
func (c serverInstallContext) GitLabHosts() []string { return c.s.parseOpts().GitLabHosts }
func (c serverInstallContext) AzureHosts() []string { return c.s.parseOpts().AzureHosts }
func (c serverInstallContext) CNBHosts() []string { return c.s.parseOpts().CNBHosts }
func (c serverInstallContext) GiteaHosts() []string { return c.s.parseOpts().GiteaHosts }
type configEntryInfo struct {
Name string `json:"name"`
Source string `json:"source"`
Tracked bool `json:"tracked"`
Branch string `json:"branch,omitempty"`
}
// handleMissingConfigEntries lists config entries that are not on disk, so the
// dashboard can offer to install them (bare `skillshare install`).
func (s *Server) handleMissingConfigEntries(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
missing := install.MissingFromConfig(serverInstallContext{s})
s.mu.RUnlock()
entries := make([]configEntryInfo, 0, len(missing))
for _, m := range missing {
entries = append(entries, configEntryInfo{Name: m.FullName(), Source: m.Source, Tracked: m.Tracked, Branch: m.Branch})
}
file := ".metadata.json"
if s.IsProjectMode() {
file = ".skillshare/config.yaml"
}
writeJSON(w, map[string]any{"entries": entries, "file": file})
}
// handleInstallFromConfig installs every config entry missing on disk, like
// bare `skillshare install`.
func (s *Server) handleInstallFromConfig(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
defer s.trackProjectLock()()
opts := install.InstallOptions{
Quiet: true,
AuditThreshold: s.auditThreshold(),
SourceFollow: s.skillsWalk().Follow,
}
if s.IsProjectMode() {
opts.AuditProjectRoot = s.projectRoot
lock, err := install.LoadLock(projectdir.Resolve(s.projectRoot))
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
opts.Lock = lock
}
result, err := install.InstallFromConfig(serverInstallContext{s}, opts)
status, msg := "ok", ""
if err != nil {
status, msg = "error", err.Error()
} else if len(result.FailedSkills) > 0 {
status = "partial"
}
s.writeOpsLog("install", status, start, map[string]any{
"source": "config",
"mode": s.installLogMode(),
"threshold": s.auditThreshold(),
"scope": "ui",
"installed_skills": result.InstalledSkills,
"failed_skills": result.FailedSkills,
}, msg)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
writeJSON(w, map[string]any{
"installed": result.Installed,
"installedRepos": result.InstalledRepos,
"installedRepoSkills": result.InstalledRepoSkills,
"skipped": result.Skipped,
"failed": result.FailedSkills,
})
}