| 1 | package legatus |
| 2 | |
| 3 | import "time" |
| 4 | |
| 5 | // Status is the outcome of a step or the aggregate of a flow. |
| 6 | type Status int |
| 7 | |
| 8 | const ( |
| 9 | // StatusOK — applied, no change (or all steps skipped). |
| 10 | StatusOK Status = iota |
| 11 | // StatusChanged — applied and state changed. |
| 12 | StatusChanged |
| 13 | // StatusSkipped — Check reported no change needed. |
| 14 | StatusSkipped |
| 15 | // StatusWouldChange — dry-run: Check reported a change; Apply was not run. |
| 16 | StatusWouldChange |
| 17 | // StatusFailed — Check or Apply errored. |
| 18 | StatusFailed |
| 19 | ) |
| 20 | |
| 21 | func (s Status) String() string { |
| 22 | switch s { |
| 23 | case StatusOK: |
| 24 | return "ok" |
| 25 | case StatusChanged: |
| 26 | return "changed" |
| 27 | case StatusSkipped: |
| 28 | return "skipped" |
| 29 | case StatusWouldChange: |
| 30 | return "would-change" |
| 31 | case StatusFailed: |
| 32 | return "failed" |
| 33 | default: |
| 34 | return "unknown" |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // StepResult is the engine's record of one step on one host. |
| 39 | type StepResult struct { |
| 40 | Step string |
| 41 | Status Status |
| 42 | Outcome Outcome |
| 43 | Err error |
| 44 | Started time.Time |
| 45 | Duration time.Duration |
| 46 | } |
| 47 | |
| 48 | // FlowResult is the record of one flow on one host, with a snapshot of the final |
| 49 | // param bag for pulling forward-passed data back out. |
| 50 | type FlowResult struct { |
| 51 | Flow string |
| 52 | Host string |
| 53 | Steps []StepResult |
| 54 | Status Status |
| 55 | Params map[string]any |
| 56 | } |
| 57 | |
| 58 | // RunResult aggregates one FlowResult per host. |
| 59 | type RunResult struct { |
| 60 | Flows []FlowResult |
| 61 | } |
| 62 | |
| 63 | // Failed reports whether any host's flow failed. |
| 64 | func (r RunResult) Failed() bool { |
| 65 | for _, f := range r.Flows { |
| 66 | if f.Status == StatusFailed { |
| 67 | return true |
| 68 | } |
| 69 | } |
| 70 | return false |
| 71 | } |
| 72 | |
| 73 | // Counts tallies step statuses across all hosts. WouldChange counts as changed. |
| 74 | func (r RunResult) Counts() (ok, changed, skipped, failed int) { |
| 75 | for _, f := range r.Flows { |
| 76 | for _, s := range f.Steps { |
| 77 | switch s.Status { |
| 78 | case StatusOK: |
| 79 | ok++ |
| 80 | case StatusChanged, StatusWouldChange: |
| 81 | changed++ |
| 82 | case StatusSkipped: |
| 83 | skipped++ |
| 84 | case StatusFailed: |
| 85 | failed++ |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | // aggregate derives a flow's Status from its steps: Failed > Changed > |
| 93 | // WouldChange > OK; Skipped does not elevate. |
| 94 | func aggregate(steps []StepResult) Status { |
| 95 | st := StatusOK |
| 96 | for _, s := range steps { |
| 97 | switch s.Status { |
| 98 | case StatusFailed: |
| 99 | return StatusFailed |
| 100 | case StatusChanged: |
| 101 | st = StatusChanged |
| 102 | case StatusWouldChange: |
| 103 | if st != StatusChanged { |
| 104 | st = StatusWouldChange |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | return st |
| 109 | } |
| 110 | |