runtimeclass-debugger/pkg/diag/report.go

57 lines
1.4 KiB
Go

package diag
import (
"encoding/json"
"fmt"
"strings"
)
const reportTitle = "RuntimeClass Edge Diagnostics"
// Table renders the human-readable report.
func (r *Report) Table() string {
var b strings.Builder
b.WriteString(reportTitle + "\n")
b.WriteString(strings.Repeat("=", len(reportTitle)) + "\n")
for _, c := range r.Checks {
fmt.Fprintf(&b, "[%s] %-10s %s\n", strings.ToUpper(string(c.Status)), c.ID, c.Detail)
}
b.WriteString("\nResult: " + r.summary() + "\n")
return b.String()
}
func (r *Report) summary() string {
counts := map[Status]int{}
for _, c := range r.Checks {
counts[c.Status]++
}
var parts []string
for _, s := range []Status{StatusPass, StatusFail, StatusWarn, StatusSkip} {
if n := counts[s]; n > 0 {
parts = append(parts, fmt.Sprintf("%d %s", n, s))
}
}
return strings.Join(parts, ", ")
}
// JSON renders the machine-readable report.
func (r *Report) JSON() (string, error) {
type checkJSON struct {
ID string `json:"id"`
Status string `json:"status"`
Detail string `json:"detail"`
}
type reportJSON struct {
Checks []checkJSON `json:"checks"`
ExitCode int `json:"exitCode"`
}
out := reportJSON{ExitCode: r.ExitCode}
for _, c := range r.Checks {
out.Checks = append(out.Checks, checkJSON{ID: c.ID, Status: string(c.Status), Detail: c.Detail})
}
b, err := json.MarshalIndent(out, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}