mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
The doctor handler shells out to the CLI binary. In global mode it did not pass -g, so the subprocess auto-detected project mode when the server's cwd contained .skillshare/config.yaml. This caused the doctor to check the project source instead of the global source.
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
// TODO: Move doctor check logic from cmd/skillshare/ to internal/doctor/
|
|
// so the server can call it directly instead of shelling out to the CLI binary.
|
|
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
func (s *Server) handleDoctor(w http.ResponseWriter, r *http.Request) {
|
|
args := []string{"doctor", "--json"}
|
|
|
|
if s.IsProjectMode() {
|
|
args = append(args, "-p")
|
|
} else {
|
|
args = append(args, "-g")
|
|
}
|
|
|
|
path, err := os.Executable()
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "cannot determine binary path: "+err.Error())
|
|
return
|
|
}
|
|
|
|
cmd := exec.Command(path, args...)
|
|
if s.IsProjectMode() {
|
|
cmd.Dir = s.projectRoot
|
|
}
|
|
|
|
cmd.Env = append(os.Environ(), "SKILLSHARE_CONFIG="+s.configPath())
|
|
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
// doctor --json writes valid JSON to stdout even on error (exit 1)
|
|
if len(output) > 0 {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(output)
|
|
return
|
|
}
|
|
writeError(w, http.StatusInternalServerError, "doctor check failed: "+err.Error())
|
|
return
|
|
}
|
|
|
|
// Validate JSON before proxying
|
|
var raw json.RawMessage
|
|
if json.Unmarshal(output, &raw) != nil {
|
|
writeError(w, http.StatusInternalServerError, "doctor produced invalid JSON")
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(output)
|
|
}
|