-
Notifications
You must be signed in to change notification settings - Fork 4
/
visitor_validator.go
77 lines (63 loc) · 1.65 KB
/
visitor_validator.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
package model
import (
"errors"
"fmt"
"github.com/robfig/cron/v3"
"go.uber.org/multierr"
)
type validator struct {
err error
}
func (v *validator) Err() error {
return v.err
}
func (v *validator) Visit(node Node) Visitor {
switch n := node.(type) {
case *HTTPServer:
if len(n.Port) == 0 {
multierr.AppendInto(&v.err, errors.New("http_server port missing"))
}
case *GrpcServer:
if len(n.Address) == 0 {
multierr.AppendInto(&v.err, errors.New("grpc_server address missing"))
}
case *Program:
if len(n.Name) == 0 {
multierr.AppendInto(&v.err, errors.New("program name missing"))
// won't process anymore, as we have no name
return v
}
if len(n.Cron) > 0 {
p := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)
_, err := p.Parse(n.Cron)
if err != nil {
multierr.AppendInto(&v.err, fmt.Errorf("error parsing cron for program %q, cron %q: %w", n.Name, n.Cron, err))
}
}
if len(n.Command) == 0 {
multierr.AppendInto(&v.err, fmt.Errorf("missing command for program %q", n.Name))
}
case *FileSystem:
if len(n.Root) == 0 {
multierr.AppendInto(&v.err, errors.New("filesystem.root is required"))
}
for _, f := range n.Files {
if len(f.Name) == 0 {
multierr.AppendInto(&v.err, errors.New("file name missing"))
continue
}
if len(f.Path) == 0 {
multierr.AppendInto(&v.err, fmt.Errorf("path missing for file: %s", f.Name))
}
if len(f.Content) == 0 {
multierr.AppendInto(&v.err, fmt.Errorf("content missing for file: %s", f.Name))
}
}
}
return v
}
func Validate(m *Root) error {
var v validator
Walk(&v, m)
return v.Err()
}