-
Notifications
You must be signed in to change notification settings - Fork 159
/
rule_env_var.go
62 lines (55 loc) · 1.43 KB
/
rule_env_var.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
package actionlint
import "strings"
// RuleEnvVar is a rule checker to check environment variables setup.
type RuleEnvVar struct {
RuleBase
}
// NewRuleEnvVar creates new RuleEnvVar instance.
func NewRuleEnvVar() *RuleEnvVar {
return &RuleEnvVar{
RuleBase: RuleBase{
name: "env-var",
desc: "Checks for environment variables configuration at \"env:\"",
},
}
}
// VisitStep is callback when visiting Step node.
func (rule *RuleEnvVar) VisitStep(n *Step) error {
rule.checkEnv(n.Env)
return nil
}
// VisitJobPre is callback when visiting Job node before visiting its children.
func (rule *RuleEnvVar) VisitJobPre(n *Job) error {
rule.checkEnv(n.Env)
if n.Container != nil {
rule.checkEnv(n.Container.Env)
}
if n.Services != nil {
for _, s := range n.Services.Value {
rule.checkEnv(s.Container.Env)
}
}
return nil
}
// VisitWorkflowPre is callback when visiting Workflow node before visiting its children.
func (rule *RuleEnvVar) VisitWorkflowPre(n *Workflow) error {
rule.checkEnv(n.Env)
return nil
}
func (rule *RuleEnvVar) checkEnv(env *Env) {
if env == nil || env.Expression != nil {
return
}
for _, v := range env.Vars {
if v.Name.ContainsExpression() {
continue // Key name can contain expressions (#312)
}
if strings.ContainsAny(v.Name.Value, "&= ") {
rule.Errorf(
v.Name.Pos,
"environment variable name %q is invalid. '&', '=' and spaces should not be contained",
v.Name.Value,
)
}
}
}