|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package preflight |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | +) |
| 9 | + |
| 10 | +// Category groups checks of the same concern so callers can opt in by |
| 11 | +// category via CLI flags. New categories are added as new check sets land — |
| 12 | +// see docs/migration_preflight_issue.md for the planned ones. |
| 13 | +type Category string |
| 14 | + |
| 15 | +const ( |
| 16 | + CategoryConnectivity Category = "connectivity" |
| 17 | +) |
| 18 | + |
| 19 | +// Finding describes a single issue detected by a Check. Every finding is an |
| 20 | +// error — a check that finds nothing wrong returns no findings at all. |
| 21 | +type Finding struct { |
| 22 | + Message string `json:"message"` |
| 23 | +} |
| 24 | + |
| 25 | +// Check is the minimal contract every preflight check must satisfy. Run returns |
| 26 | +// the findings the check produced; a non-nil error means the check itself could |
| 27 | +// not complete (distinct from finding a problem with the system under test). |
| 28 | +type Check interface { |
| 29 | + Name() string |
| 30 | + Run(ctx context.Context) ([]Finding, error) |
| 31 | +} |
| 32 | + |
| 33 | +// CheckResult bundles a check's name with whatever it produced. |
| 34 | +type CheckResult struct { |
| 35 | + Name string `json:"name"` |
| 36 | + Findings []Finding `json:"findings"` |
| 37 | + Err error `json:"-"` |
| 38 | +} |
| 39 | + |
| 40 | +// MarshalJSON renders Err as a string so the report is consumable from a |
| 41 | +// non-Go process. The default error marshaling drops the message. |
| 42 | +func (r CheckResult) MarshalJSON() ([]byte, error) { |
| 43 | + out := struct { |
| 44 | + Name string `json:"name"` |
| 45 | + Findings []Finding `json:"findings"` |
| 46 | + Error string `json:"error,omitempty"` |
| 47 | + }{ |
| 48 | + Name: r.Name, |
| 49 | + Findings: r.Findings, |
| 50 | + } |
| 51 | + if r.Err != nil { |
| 52 | + out.Error = r.Err.Error() |
| 53 | + } |
| 54 | + return json.Marshal(out) |
| 55 | +} |
| 56 | + |
| 57 | +// Report is the outcome of running a set of checks. |
| 58 | +type Report struct { |
| 59 | + Results []CheckResult `json:"results"` |
| 60 | +} |
| 61 | + |
| 62 | +// ProgressFunc is invoked just before each check runs. idx is 1-based. |
| 63 | +type ProgressFunc func(idx, total int, name string) |
| 64 | + |
| 65 | +// RunOption configures Run. |
| 66 | +type RunOption func(*runOptions) |
| 67 | + |
| 68 | +type runOptions struct { |
| 69 | + progress ProgressFunc |
| 70 | +} |
| 71 | + |
| 72 | +// WithProgress installs a callback invoked before each check runs. Useful for |
| 73 | +// updating a spinner or log line with "running X of N: <name>". |
| 74 | +func WithProgress(fn ProgressFunc) RunOption { |
| 75 | + return func(o *runOptions) { o.progress = fn } |
| 76 | +} |
| 77 | + |
| 78 | +// Run executes every check in order. A check returning an error does not stop |
| 79 | +// the run; subsequent checks still execute and the error is captured in the |
| 80 | +// report alongside the findings. |
| 81 | +func Run(ctx context.Context, checks []Check, opts ...RunOption) Report { |
| 82 | + var ro runOptions |
| 83 | + for _, opt := range opts { |
| 84 | + opt(&ro) |
| 85 | + } |
| 86 | + |
| 87 | + results := make([]CheckResult, 0, len(checks)) |
| 88 | + total := len(checks) |
| 89 | + for i, c := range checks { |
| 90 | + if ro.progress != nil { |
| 91 | + ro.progress(i+1, total, c.Name()) |
| 92 | + } |
| 93 | + findings, err := c.Run(ctx) |
| 94 | + results = append(results, CheckResult{ |
| 95 | + Name: c.Name(), |
| 96 | + Findings: findings, |
| 97 | + Err: err, |
| 98 | + }) |
| 99 | + } |
| 100 | + return Report{Results: results} |
| 101 | +} |
| 102 | + |
| 103 | +// HasErrors reports whether any check produced findings or failed to complete. |
| 104 | +func (r Report) HasErrors() bool { |
| 105 | + for _, res := range r.Results { |
| 106 | + if res.Err != nil || len(res.Findings) > 0 { |
| 107 | + return true |
| 108 | + } |
| 109 | + } |
| 110 | + return false |
| 111 | +} |
0 commit comments