-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (147 loc) · 3.36 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"errors"
"fmt"
"io"
"os"
"github.com/docker/docker/builder/dockerfile/parser"
"github.com/t-ashula/dflint/rule"
"github.com/urfave/cli"
)
var Version string
type lintOption struct {
IgnoreRules rule.Rules
ShellCheckPath string
}
func main() {
app := setupApp()
app.Run(os.Args)
}
func setupApp() *cli.App {
app := cli.NewApp()
app.Name = "dflint"
app.Version = Version
app.Flags = []cli.Flag{
cli.StringSliceFlag{
Name: "ignore, i",
Usage: "ignore rules",
},
cli.StringFlag{
Name: "shellcheck",
Usage: "set ShellCheck Path",
},
}
app.Usage = "lint Dockerfile"
app.UsageText = "dflint [global options] Dockerfile [Dockerfile,...]"
app.Action = realMain
return app
}
func realMain(c *cli.Context) error {
if c.NArg() < 1 {
return errors.New("at least one Dockerfile required")
}
knownRules := rule.GetRules()
ignoreNames := c.StringSlice("ignore")
ignoreRules, errorIds := checkIgnoreRule(knownRules, ignoreNames)
if errorIds != nil {
for _, id := range errorIds {
fmt.Fprintf(os.Stderr, "Ignored Unknown Rule ID: %s\n", id)
}
}
scpath := c.String("shellcheck")
if scpath == "" {
autopath, err := shellCheckPath()
if err == nil {
scpath = autopath
}
}
opts := &lintOption{
IgnoreRules: ignoreRules,
ShellCheckPath: scpath,
}
files := c.Args()
for _, file := range files {
result, err := lint(file, opts)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s, %s\n", file, err)
continue
}
report(result)
}
return nil
}
func checkIgnoreRule(rules rule.Rules, names []string) (rule.Rules, []string) {
uniqNames := map[string]bool{}
for _, s := range names {
uniqNames[s] = true
}
var ignores rule.Rules
var unknowns []string
for s := range uniqNames {
r := rules.Find(rule.IsNameMatch(s))
if r != nil {
ignores = append(ignores, r)
} else {
unknowns = append(unknowns, s)
}
}
return ignores, unknowns
}
func lint(path string, opts *lintOption) (results rule.Results, err error) {
_, err = os.Stat(path)
if err != nil {
return nil, err
}
fp, err := os.Open(path)
if err != nil {
return nil, err
}
defer fp.Close()
ast, err := buildAST(fp)
if err != nil {
fmt.Printf("%#v\n", err)
return nil, err
}
rules := rule.GetRules()
if opts.ShellCheckPath != "" {
rule.RegisterRule(shellCheckRule(opts.ShellCheckPath))
}
results, err = check(ast, rules)
if err != nil {
return nil, err
}
results, err = filterResults(results, opts.IgnoreRules)
if err != nil {
return nil, err
}
return results, nil
}
func buildAST(fp io.Reader) (*parser.Node, error) {
d := parser.Directive{
EscapeSeen: false,
LookingForDirectives: true,
}
parser.SetEscapeToken(parser.DefaultEscapeToken, &d)
return parser.Parse(fp, &d)
}
func check(ast *parser.Node, rules rule.Rules) (rule.Results, error) {
for _, r := range rules {
r.Validate(ast)
}
return rule.GetResults(), nil
}
func filterResults(results rule.Results, ignores rule.Rules) (rule.Results, error) {
var tmp rule.Results
for _, result := range results {
r := ignores.Find(rule.IsNameMatch(result.Rule.Name))
if r == nil {
tmp = append(tmp, result)
}
}
return tmp, nil
}
func report(results rule.Results) {
for i, res := range results {
fmt.Printf("%d: %d,%d,%d,%d: %s: %s\n", i, res.StartLine, res.StartColumn, res.EndLine, res.EndColumn, res.Rule.Name, res.Rule.Description)
}
}