-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
68 lines (60 loc) · 1.84 KB
/
validation.go
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
package verdeter
import (
"fmt"
)
// Validate checks if config keys have valid values
func (verdeterCmd *VerdeterCommand) Validate(isTargetCommand bool) error {
// Validate parent command
if verdeterCmd.parentCmd != nil {
err := verdeterCmd.parentCmd.Validate(false)
if err != nil {
return fmt.Errorf("(from %q) an error happened while verifying parent command %q : %w", verdeterCmd.cmd.Name(), verdeterCmd.parentCmd.cmd.Name(), err)
}
}
listErrors := make([]error, 0)
// validate global config keys
listErrors = validateGlobalKeys(verdeterCmd, listErrors)
// validate local config keys
if isTargetCommand {
listErrors = validateLocalKeys(verdeterCmd, listErrors)
}
for constraintName, constraint := range verdeterCmd.constraints {
if !constraint() {
listErrors = append(listErrors,
fmt.Errorf("constraint %q is not respected", constraintName),
)
}
}
if len(listErrors) != 0 {
printErrors(listErrors)
return fmt.Errorf("validation failed (%d errors)", len(listErrors))
}
return nil
}
// validateLocalKeys: validate global config keys
func validateLocalKeys(verdeterCmd *VerdeterCommand, listErrors []error) []error {
for _, configKey := range verdeterCmd.localConfigKeys {
errs := configKey.Validate()
if errs != nil {
listErrors = append(listErrors, errs...)
}
}
return listErrors
}
// validateGlobalKeys: validate local config keys
func validateGlobalKeys(verdeterCmd *VerdeterCommand, listErrors []error) []error {
for _, configKey := range verdeterCmd.globalConfigKeys {
errs := configKey.Validate()
if errs != nil {
listErrors = append(listErrors, errs...)
}
}
return listErrors
}
// printErrors print the error in a clean way.
func printErrors(listErrors []error) {
fmt.Println("Some errors were collected during initialization:")
for _, err := range listErrors {
fmt.Println("-", err.Error())
}
}