|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os/exec" |
| 9 | + "runtime" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/RamazanKara/openexit/internal/version" |
| 14 | + publicschemas "github.com/RamazanKara/openexit/schemas" |
| 15 | + "github.com/santhosh-tekuri/jsonschema/v6" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +type DoctorReport struct { |
| 20 | + Status string `json:"status"` |
| 21 | + Checks []DoctorCheck `json:"checks"` |
| 22 | +} |
| 23 | + |
| 24 | +type DoctorCheck struct { |
| 25 | + Name string `json:"name"` |
| 26 | + Status string `json:"status"` |
| 27 | + Message string `json:"message,omitempty"` |
| 28 | +} |
| 29 | + |
| 30 | +func newDoctorCommand() *cobra.Command { |
| 31 | + var jsonOutput, strict bool |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "doctor", |
| 34 | + Short: "Check local OpenExit runtime readiness", |
| 35 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | + report := runDoctor() |
| 37 | + if jsonOutput { |
| 38 | + enc := json.NewEncoder(cmd.OutOrStdout()) |
| 39 | + enc.SetIndent("", " ") |
| 40 | + if err := enc.Encode(report); err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + } else { |
| 44 | + writeDoctorReport(cmd.OutOrStdout(), report) |
| 45 | + } |
| 46 | + if report.Status == "failed" || (strict && report.Status == "warning") { |
| 47 | + return fmt.Errorf("doctor status: %s", report.Status) |
| 48 | + } |
| 49 | + return nil |
| 50 | + }, |
| 51 | + } |
| 52 | + cmd.Flags().BoolVar(&jsonOutput, "json", false, "Write machine-readable doctor report") |
| 53 | + cmd.Flags().BoolVar(&strict, "strict", false, "Treat warnings as failures") |
| 54 | + return cmd |
| 55 | +} |
| 56 | + |
| 57 | +func runDoctor() DoctorReport { |
| 58 | + report := DoctorReport{Status: "passed"} |
| 59 | + report.Checks = append(report.Checks, |
| 60 | + versionMetadataCheck(), |
| 61 | + schemaBundleCheck(), |
| 62 | + optionalToolCheck("promtool", "Prometheus rule syntax validation"), |
| 63 | + optionalToolCheck("kubeconform", "Kubernetes manifest validation"), |
| 64 | + ) |
| 65 | + sort.SliceStable(report.Checks, func(i, j int) bool { return report.Checks[i].Name < report.Checks[j].Name }) |
| 66 | + for _, check := range report.Checks { |
| 67 | + switch check.Status { |
| 68 | + case "failed": |
| 69 | + report.Status = "failed" |
| 70 | + case "warning": |
| 71 | + if report.Status == "passed" { |
| 72 | + report.Status = "warning" |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return report |
| 77 | +} |
| 78 | + |
| 79 | +func versionMetadataCheck() DoctorCheck { |
| 80 | + var missing []string |
| 81 | + for key, value := range map[string]string{ |
| 82 | + "name": version.Name, |
| 83 | + "version": version.Version, |
| 84 | + "commit": version.Commit, |
| 85 | + "date": version.Date, |
| 86 | + } { |
| 87 | + if strings.TrimSpace(value) == "" { |
| 88 | + missing = append(missing, key) |
| 89 | + } |
| 90 | + } |
| 91 | + if len(missing) > 0 { |
| 92 | + sort.Strings(missing) |
| 93 | + return DoctorCheck{Name: "version-metadata", Status: "failed", Message: "missing " + strings.Join(missing, ", ")} |
| 94 | + } |
| 95 | + var unstamped []string |
| 96 | + if version.Commit == "unknown" { |
| 97 | + unstamped = append(unstamped, "commit") |
| 98 | + } |
| 99 | + if version.Date == "unknown" { |
| 100 | + unstamped = append(unstamped, "date") |
| 101 | + } |
| 102 | + if len(unstamped) > 0 { |
| 103 | + return DoctorCheck{ |
| 104 | + Name: "version-metadata", |
| 105 | + Status: "warning", |
| 106 | + Message: fmt.Sprintf("unstamped %s in %s %s %s/%s", strings.Join(unstamped, ", "), version.Name, version.Version, runtime.GOOS, runtime.GOARCH), |
| 107 | + } |
| 108 | + } |
| 109 | + return DoctorCheck{ |
| 110 | + Name: "version-metadata", |
| 111 | + Status: "passed", |
| 112 | + Message: fmt.Sprintf("%s %s %s/%s commit=%s date=%s", version.Name, version.Version, runtime.GOOS, runtime.GOARCH, version.Commit, version.Date), |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func schemaBundleCheck() DoctorCheck { |
| 117 | + compiler := jsonschema.NewCompiler() |
| 118 | + compiler.DefaultDraft(jsonschema.Draft7) |
| 119 | + entries, err := publicschemas.FS.ReadDir(".") |
| 120 | + if err != nil { |
| 121 | + return DoctorCheck{Name: "schema-bundle", Status: "failed", Message: err.Error()} |
| 122 | + } |
| 123 | + var names []string |
| 124 | + for _, entry := range entries { |
| 125 | + if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".schema.json") { |
| 126 | + continue |
| 127 | + } |
| 128 | + data, err := publicschemas.FS.ReadFile(entry.Name()) |
| 129 | + if err != nil { |
| 130 | + return DoctorCheck{Name: "schema-bundle", Status: "failed", Message: err.Error()} |
| 131 | + } |
| 132 | + document, err := jsonschema.UnmarshalJSON(bytes.NewReader(data)) |
| 133 | + if err != nil { |
| 134 | + return DoctorCheck{Name: "schema-bundle", Status: "failed", Message: fmt.Sprintf("%s: %v", entry.Name(), err)} |
| 135 | + } |
| 136 | + if err := compiler.AddResource(entry.Name(), document); err != nil { |
| 137 | + return DoctorCheck{Name: "schema-bundle", Status: "failed", Message: fmt.Sprintf("%s: %v", entry.Name(), err)} |
| 138 | + } |
| 139 | + names = append(names, entry.Name()) |
| 140 | + } |
| 141 | + for _, name := range names { |
| 142 | + if _, err := compiler.Compile(name); err != nil { |
| 143 | + return DoctorCheck{Name: "schema-bundle", Status: "failed", Message: fmt.Sprintf("%s: %v", name, err)} |
| 144 | + } |
| 145 | + } |
| 146 | + return DoctorCheck{Name: "schema-bundle", Status: "passed", Message: fmt.Sprintf("%d embedded schema(s) compiled", len(names))} |
| 147 | +} |
| 148 | + |
| 149 | +func optionalToolCheck(name, purpose string) DoctorCheck { |
| 150 | + path, err := exec.LookPath(name) |
| 151 | + if err != nil { |
| 152 | + return DoctorCheck{Name: name, Status: "warning", Message: purpose + " unavailable; install " + name + " for stronger validation"} |
| 153 | + } |
| 154 | + return DoctorCheck{Name: name, Status: "passed", Message: path} |
| 155 | +} |
| 156 | + |
| 157 | +func writeDoctorReport(w io.Writer, report DoctorReport) { |
| 158 | + _, _ = fmt.Fprintf(w, "status: %s\n", report.Status) |
| 159 | + for _, check := range report.Checks { |
| 160 | + if check.Message == "" { |
| 161 | + _, _ = fmt.Fprintf(w, "%s: %s\n", check.Name, check.Status) |
| 162 | + continue |
| 163 | + } |
| 164 | + _, _ = fmt.Fprintf(w, "%s: %s - %s\n", check.Name, check.Status, check.Message) |
| 165 | + } |
| 166 | +} |
0 commit comments