Decode final Jeannie report object
Homelab Main / validate (push) Failing after 1m45s Details
Homelab Main / running-update (push) Has been skipped Details
Homelab Main / debian-ops (push) Has been skipped Details

This commit is contained in:
juvenal.diaz 2026-12-05 08:44:35 -06:00
parent 68b3d53a61
commit f32c1da60d
2 changed files with 37 additions and 11 deletions

View File

@ -312,13 +312,8 @@ func (client *JeannieClient) request(ctx context.Context, method string, path st
}
func decodeStatusOutput(output string) (StatusSnapshot, error) {
start := strings.Index(output, "{")
if start < 0 {
return StatusSnapshot{}, errors.New("Jeannie status output did not contain JSON")
}
var snapshot StatusSnapshot
if err := json.Unmarshal([]byte(output[start:]), &snapshot); err != nil {
if err := decodeFinalJSONObject(output, &snapshot); err != nil {
return StatusSnapshot{}, fmt.Errorf("decode Jeannie status output: %w", err)
}
if snapshot.Rows == nil {
@ -340,11 +335,7 @@ func decodeDoctorOutput(output string) (IncidentSnapshot, error) {
Title string `json:"title"`
Rows []doctorRow `json:"rows"`
}
start := strings.Index(output, "{")
if start < 0 {
return IncidentSnapshot{}, errors.New("Jeannie Doctor output did not contain JSON")
}
if err := json.Unmarshal([]byte(output[start:]), &report); err != nil {
if err := decodeFinalJSONObject(output, &report); err != nil {
return IncidentSnapshot{}, fmt.Errorf("decode Jeannie Doctor output: %w", err)
}
if report.Rows == nil {
@ -372,6 +363,29 @@ func decodeDoctorOutput(output string) (IncidentSnapshot, error) {
return snapshot, nil
}
func decodeFinalJSONObject(output string, target any) error {
searchEnd := len(output)
for searchEnd > 0 {
start := strings.LastIndex(output[:searchEnd], "{")
if start < 0 {
break
}
candidate := strings.TrimSpace(output[start:])
var raw json.RawMessage
decoder := json.NewDecoder(strings.NewReader(candidate))
if err := decoder.Decode(&raw); err == nil {
var trailing any
if err := decoder.Decode(&trailing); errors.Is(err, io.EOF) {
if err := json.Unmarshal(raw, target); err == nil {
return nil
}
}
}
searchEnd = start
}
return errors.New("output did not contain a complete JSON object")
}
func cleanExcerpt(value string, limit int) string {
value = strings.Map(func(character rune) rune {
if character == '\n' || character == '\t' || character >= 32 {

View File

@ -66,6 +66,18 @@ func TestDecodeStatusOutputRejectsNonJSON(t *testing.T) {
}
}
func TestDecodeStatusOutputUsesFinalCompleteJSONObject(t *testing.T) {
output := "probe emitted {\"message\":\"broken\nline\"}\n" +
`{"title":"Jeannie Status: all","mode":"fast","cluster":{"profile":"k3s","runtime":"k3s"},"rows":[{"status":"ok","area":"Kubernetes","check":"API readiness","summary":"ok"}]}`
snapshot, err := decodeStatusOutput(output)
if err != nil {
t.Fatal(err)
}
if snapshot.Title != "Jeannie Status: all" || len(snapshot.Rows) != 1 || snapshot.Rows[0].Check != "API readiness" {
t.Fatalf("unexpected snapshot: %#v", snapshot)
}
}
func TestDoctorNormalizesEvidenceAndRejectsArbitraryTargets(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
switch request.URL.Path {