-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.go
More file actions
91 lines (79 loc) · 1.83 KB
/
Copy pathconfig.go
File metadata and controls
91 lines (79 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package doctor
import (
"context"
"errors"
"github.com/hay-kot/criterio"
"github.com/hay-kot/hive/internal/core/config"
)
// ConfigCheck validates the configuration file.
type ConfigCheck struct {
config *config.Config
configPath string
}
// NewConfigCheck creates a new configuration check.
func NewConfigCheck(cfg *config.Config, configPath string) *ConfigCheck {
return &ConfigCheck{
config: cfg,
configPath: configPath,
}
}
func (c *ConfigCheck) Name() string {
return "Configuration"
}
func (c *ConfigCheck) Run(ctx context.Context) Result {
result := Result{Name: c.Name()}
if c.config == nil {
result.Items = append(result.Items, CheckItem{
Label: "Config loaded",
Status: StatusFail,
Detail: "configuration not loaded",
})
return result
}
err := c.config.ValidateDeep(c.configPath)
warnings := c.config.Warnings()
// If no errors and no warnings, report success
if err == nil && len(warnings) == 0 {
result.Items = append(result.Items, CheckItem{
Label: "Config valid",
Status: StatusPass,
})
return result
}
// Extract and report errors
if err != nil {
var fieldErrs criterio.FieldErrors
if errors.As(err, &fieldErrs) {
for _, fe := range fieldErrs {
label := fe.Field
if label == "" {
label = "validation"
}
result.Items = append(result.Items, CheckItem{
Label: label,
Status: StatusFail,
Detail: fe.Err.Error(),
})
}
} else {
result.Items = append(result.Items, CheckItem{
Label: "validation",
Status: StatusFail,
Detail: err.Error(),
})
}
}
// Extract and report warnings
for _, w := range warnings {
label := w.Category
if w.Item != "" {
label += " (" + w.Item + ")"
}
result.Items = append(result.Items, CheckItem{
Label: label,
Status: StatusWarn,
Detail: w.Message,
})
}
return result
}