-
Notifications
You must be signed in to change notification settings - Fork 158
/
rule.go
108 lines (90 loc) · 3.23 KB
/
rule.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
package actionlint
import (
"fmt"
"io"
)
// RuleBase is a struct to be a base of rule structs. Embed this struct to define default methods
// automatically
type RuleBase struct {
name string
desc string
errs []*Error
dbg io.Writer
config *Config
}
// NewRuleBase creates a new RuleBase instance. It should be embedded to your own
// rule instance.
func NewRuleBase(name string, desc string) RuleBase {
return RuleBase{
name: name,
desc: desc,
}
}
// VisitStep is callback when visiting Step node.
func (r *RuleBase) VisitStep(node *Step) error { return nil }
// VisitJobPre is callback when visiting Job node before visiting its children.
func (r *RuleBase) VisitJobPre(node *Job) error { return nil }
// VisitJobPost is callback when visiting Job node after visiting its children.
func (r *RuleBase) VisitJobPost(node *Job) error { return nil }
// VisitWorkflowPre is callback when visiting Workflow node before visiting its children.
func (r *RuleBase) VisitWorkflowPre(node *Workflow) error { return nil }
// VisitWorkflowPost is callback when visiting Workflow node after visiting its children.
func (r *RuleBase) VisitWorkflowPost(node *Workflow) error { return nil }
// Error creates a new error from the source position and the error message and stores it in the
// rule instance. The errors can be accessed by Errs method.
func (r *RuleBase) Error(pos *Pos, msg string) {
err := errorAt(pos, r.name, msg)
r.errs = append(r.errs, err)
}
// Errorf reports a new error with the source position and the formatted error message and stores it
// in the rule instance. The errors can be accessed by Errs method.
func (r *RuleBase) Errorf(pos *Pos, format string, args ...interface{}) {
err := errorfAt(pos, r.name, format, args...)
r.errs = append(r.errs, err)
}
// Debug prints debug log to the output. The output is specified by the argument of EnableDebug method.
// By default, no output is set so debug log is not printed.
func (r *RuleBase) Debug(format string, args ...interface{}) {
if r.dbg == nil {
return
}
format = fmt.Sprintf("[%s] %s\n", r.name, format)
fmt.Fprintf(r.dbg, format, args...)
}
// Errs returns errors found by the rule.
func (r *RuleBase) Errs() []*Error {
return r.errs
}
// Name returns the name of the rule.
func (r *RuleBase) Name() string {
return r.name
}
// Description returns the description of the rule.
func (r *RuleBase) Description() string {
return r.desc
}
// EnableDebug enables debug output from the rule. Given io.Writer instance is used to print debug
// information to console. Setting nil means disabling debug output.
func (r *RuleBase) EnableDebug(out io.Writer) {
r.dbg = out
}
// SetConfig populates user configuration of actionlint to the rule. When no config is set, rules
// should behave as if the default configuration is set.
func (r *RuleBase) SetConfig(cfg *Config) {
r.config = cfg
}
// Config returns the user configuration of actionlint. When no config was set to this rule by SetConfig,
// this method returns nil.
func (r *RuleBase) Config() *Config {
return r.config
}
// Rule is an interface which all rule structs must meet.
type Rule interface {
Pass
Errs() []*Error
Name() string
Description() string
EnableDebug(out io.Writer)
SetConfig(cfg *Config)
Config() *Config
}