-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
134 lines (115 loc) · 2.93 KB
/
validate.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package libocfl
import (
"github.com/rs/zerolog/log"
"os"
"path"
"sync"
)
type OCFLRule struct {
Code string
Message string
CheckError func(obj *OCFLObject) bool
CheckWarning func(obj *OCFLObject) bool
}
var e001Check = &OCFLRule{
Code: "E001",
Message: "Missing OCFL NamAsTe file",
CheckError: func(obj *OCFLObject) bool {
fpth := path.Join(obj.path, "0=ocfl_object_1.0")
_, err := os.Stat(fpth)
if err != nil {
return false
}
return true
},
}
var e002Check = &OCFLRule{
Code: "E002",
Message: "Namaste file name does not match Inventory OCFL Version",
CheckError: func(obj *OCFLObject) bool {
log.Debug().Msg("Checking E002")
return true
},
}
var e003Check = &OCFLRule{
Code: "E003",
Message: "Missing inventory.json in object root",
CheckError: func(obj *OCFLObject) bool {
log.Debug().Msg("Checking E003")
return true
},
}
var w001Check = &OCFLRule{
Code: "W001",
Message: "Some warning",
CheckWarning: func(obj *OCFLObject) bool {
return false
},
}
// Gather all the error checks into a slice
var errorChecks = []*OCFLRule{
e001Check,
e002Check,
e003Check,
}
// Gather all the warning checks into a slice
var warnChecks = []*OCFLRule{
w001Check,
}
func runValidation(obj *OCFLObject) {
ruleChannel := make(chan *OCFLRule)
var wg sync.WaitGroup
// Create up to NUM workers, each responsible for
// reading from a channel and doing work. This will continue
// until the channel is exhausted.
for worker := 0; worker < NumWorkers; worker++ {
wg.Add(1)
go func() {
defer wg.Done()
for rule := range ruleChannel {
log.Debug().Str("code", rule.Code).Msg("Queued up")
if rule.CheckError != nil {
valid := rule.CheckError(obj)
if !valid {
log.Error().
Str("code", rule.Code).
Str("path", obj.path).
Bool("valid", valid).
Msg(rule.Message)
}
} else if rule.CheckWarning != nil {
warn := rule.CheckWarning(obj)
// If we return a 'warning' status, set the
// 'warn' flag to true by inverting the returned
// status. So the warning checker returns false if
// we need to flag the result as a warning, but the
// error message will return 'warn': true.
if !warn {
log.Warn().
Str("code", rule.Code).
Str("path", obj.path).
Bool("warn", !warn).
Msg(rule.Message)
}
}
}
}()
}
log.Debug().Msg("Finished allocating workers")
// Queue up all the errors and warnings by sending them
// into the rule channel.
// Add error-check tasks to the channel
for _, errRule := range errorChecks {
ruleChannel <- errRule
}
// Add warning-check tasks to the channel
for _, warnRule := range warnChecks {
ruleChannel <- warnRule
}
// Close the rule channel. No other rules can be added, but
// the rules in the channel will be processed until there are
// no more to be done.
close(ruleChannel)
// Wait for the worker pool to finish.
wg.Wait()
}