Files
skillshare/internal/server/handler_install_config.go
T
Willie 816938c09c fix(cli): report why uninstall and install fail under --json (#835)
Scripts and the herdr panel could not tell why an uninstall or install
failed: uninstall --json had no error field, project mode ignored --json
(printing text and still prompting), and a multi-skill or config install
in which every skill was blocked exited 0.

- uninstall --json (skills and agents, global and -p) sets "error" with
  each failed name and reason; skills exit non-zero when all fail,
  agents when any fails. Project mode now emits pure JSON, including on
  the first run that initializes the project.
- uninstall -p agents parses its flags instead of treating --help or an
  unknown flag as an agent name, and uninstall agents --json is logged.
- install exits non-zero, naming each skill and reason, when every skill
  from a source or from config fails; installed or skipped skills make it
  a partial success that still exits 0. Config installs show each
  failure's reason, and the dashboard logs an all-failed run as an error.
- herdr asks for JSON in project scope and shows the CLI's error.
2026-11-10 20:24:44 +08:00

138 lines
4.5 KiB
Go

package server
import (
"net/http"
"strings"
"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 {
// Match the CLI: nothing installed is an error, with each skill's reason.
status = "partial"
if len(result.InstalledSkills)+result.Skipped == 0 {
reasons := make([]string, len(result.Failures))
for i, f := range result.Failures {
reasons[i] = f.Name + ": " + f.Err.Error()
}
status, msg = "error", strings.Join(reasons, "; ")
}
}
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,
})
}