|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "golang.org/x/xerrors" |
| 8 | +) |
| 9 | + |
| 10 | +type Checker interface { |
| 11 | + // Validate returns an error if, and only if, the Checker was not |
| 12 | + // configured correctly. |
| 13 | + // |
| 14 | + // This method is responsible for verifying that the Checker has |
| 15 | + // all required parameters and the required parameters are valid, |
| 16 | + // and that optional parameters are valid, if set. |
| 17 | + Validate() error |
| 18 | + |
| 19 | + // Run runs the checks and returns the results. |
| 20 | + // |
| 21 | + // This method will run through the checks and return results. |
| 22 | + Run(context.Context) error |
| 23 | +} |
| 24 | + |
| 25 | +var _ = fmt.Stringer(StatePassed) |
| 26 | + |
| 27 | +type CheckState int |
| 28 | + |
| 29 | +const ( |
| 30 | + // StatePassed indicates that the check passed successfully. |
| 31 | + StatePassed CheckState = iota |
| 32 | + |
| 33 | + // StateWarning indicates a condition where Coder will gracefully degrade, |
| 34 | + // but the user will not have an optimal experience. |
| 35 | + StateWarning |
| 36 | + |
| 37 | + // StateFailed indicates a condition where Coder will not be able to install |
| 38 | + // successfully. |
| 39 | + StateFailed |
| 40 | + |
| 41 | + // StateInfo indicates a result for informational or diagnostic purposes |
| 42 | + // only, with no bearing on the ability to install Coder. |
| 43 | + StateInfo |
| 44 | + |
| 45 | + // StateSkipped indicates an indeterminate result due to a skipped check. |
| 46 | + StateSkipped |
| 47 | +) |
| 48 | + |
| 49 | +func (s CheckState) MustEmoji() string { |
| 50 | + emoji, err := s.Emoji() |
| 51 | + if err != nil { |
| 52 | + panic(err.Error()) |
| 53 | + } |
| 54 | + return emoji |
| 55 | +} |
| 56 | + |
| 57 | +func (s CheckState) Emoji() (string, error) { |
| 58 | + switch s { |
| 59 | + case StatePassed: |
| 60 | + return "👍", nil |
| 61 | + case StateWarning: |
| 62 | + return "⚠️", nil |
| 63 | + case StateFailed: |
| 64 | + return "👎", nil |
| 65 | + case StateInfo: |
| 66 | + return "🔔", nil |
| 67 | + case StateSkipped: |
| 68 | + return "⏩", nil |
| 69 | + } |
| 70 | + |
| 71 | + return "", xerrors.Errorf("unknown state: %d", s) |
| 72 | +} |
| 73 | + |
| 74 | +func (s CheckState) MustText() string { |
| 75 | + text, err := s.Text() |
| 76 | + if err != nil { |
| 77 | + panic(err.Error()) |
| 78 | + } |
| 79 | + return text |
| 80 | +} |
| 81 | + |
| 82 | +func (s CheckState) Text() (string, error) { |
| 83 | + switch s { |
| 84 | + case StatePassed: |
| 85 | + return "PASS", nil |
| 86 | + case StateWarning: |
| 87 | + return "WARN", nil |
| 88 | + case StateFailed: |
| 89 | + return "FAIL", nil |
| 90 | + case StateInfo: |
| 91 | + return "INFO", nil |
| 92 | + case StateSkipped: |
| 93 | + return "SKIP", nil |
| 94 | + } |
| 95 | + |
| 96 | + return "", xerrors.Errorf("unknown state: %d", s) |
| 97 | +} |
| 98 | + |
| 99 | +func (s CheckState) String() string { |
| 100 | + switch s { |
| 101 | + case StatePassed: |
| 102 | + return "StatePassed" |
| 103 | + case StateWarning: |
| 104 | + return "StateWarning" |
| 105 | + case StateFailed: |
| 106 | + return "StateFailed" |
| 107 | + case StateInfo: |
| 108 | + return "StateInfo" |
| 109 | + case StateSkipped: |
| 110 | + return "StateSkipped" |
| 111 | + } |
| 112 | + |
| 113 | + panic(fmt.Sprintf("unknown state: %d", s)) |
| 114 | +} |
| 115 | + |
| 116 | +type CheckResult struct { |
| 117 | + Name string `json:"name"` |
| 118 | + State CheckState `json:"state"` |
| 119 | + Summary string `json:"summary"` |
| 120 | + Details map[string]interface{} `json:"details,omitempty"` |
| 121 | +} |
0 commit comments