/
id20011.go
68 lines (57 loc) · 1.52 KB
/
id20011.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 check contains the linter checks
package check
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/docker-slim/docker-slim/pkg/docker/instruction"
)
func init() {
check := &MultipleCmdInstructions{
Info: Info{
ID: "ID.20011",
Name: "Multiple CMD instructions",
Description: "Multiple CMD instructions",
DetailsURL: "https://lint.dockersl.im/check/ID.20011",
MainMessage: "Multiple CMD instructions in stage",
MatchMessage: "Instruction: start=%d end=%d global_index=%d stage_id=%d stage_index=%d",
Labels: map[string]string{
LabelLevel: LevelError,
LabelScope: ScopeStage,
},
},
}
AllChecks = append(AllChecks, check)
}
type MultipleCmdInstructions struct {
Info
}
func (c *MultipleCmdInstructions) Run(opts *Options, ctx *Context) (*Result, error) {
log.Debugf("linter.check[%s:'%s']", c.ID, c.Name)
result := &Result{
Source: &c.Info,
}
for _, stage := range ctx.Dockerfile.Stages {
if instructions, ok := stage.CurrentInstructionsByType[instruction.Cmd]; ok {
if len(instructions) > 1 {
if !result.Hit {
result.Hit = true
result.Message = c.MainMessage
}
for _, inst := range instructions {
match := &Match{
Stage: stage,
Instruction: inst,
Message: fmt.Sprintf(c.MatchMessage,
inst.StartLine,
inst.EndLine,
inst.GlobalIndex,
inst.StageID,
inst.StageIndex),
}
result.Matches = append(result.Matches, match)
}
}
}
}
return result, nil
}