Skip to content

Commit 76b7b04

Browse files
authored
Merge pull request moby#5076 from daghack/parse-lint-to-linter
Move parseLintOptions into linter package
2 parents 997156a + 4ea1b2f commit 76b7b04

File tree

3 files changed

+58
-52
lines changed

3 files changed

+58
-52
lines changed

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -169,59 +169,17 @@ func ListTargets(ctx context.Context, dt []byte) (*targets.List, error) {
169169
return l, nil
170170
}
171171

172-
func parseLintOptions(checkStr string) (*linter.Config, error) {
173-
checkStr = strings.TrimSpace(checkStr)
174-
if checkStr == "" {
175-
return &linter.Config{}, nil
176-
}
177-
178-
parts := strings.SplitN(checkStr, ";", 2)
179-
var skipSet []string
180-
var errorOnWarn, skipAll bool
181-
for _, p := range parts {
182-
k, v, ok := strings.Cut(p, "=")
183-
if !ok {
184-
return nil, errors.Errorf("invalid check option %q", p)
185-
}
186-
k = strings.TrimSpace(k)
187-
switch k {
188-
case "skip":
189-
v = strings.TrimSpace(v)
190-
if v == "all" {
191-
skipAll = true
192-
} else {
193-
skipSet = strings.Split(v, ",")
194-
for i, rule := range skipSet {
195-
skipSet[i] = strings.TrimSpace(rule)
196-
}
197-
}
198-
case "error":
199-
v, err := strconv.ParseBool(strings.TrimSpace(v))
200-
if err != nil {
201-
return nil, errors.Wrapf(err, "failed to parse check option %q", p)
202-
}
203-
errorOnWarn = v
204-
default:
205-
return nil, errors.Errorf("invalid check option %q", k)
206-
}
207-
}
208-
return &linter.Config{
209-
SkipRules: skipSet,
210-
SkipAll: skipAll,
211-
ReturnAsError: errorOnWarn,
212-
}, nil
213-
}
214-
215172
func newRuleLinter(dt []byte, opt *ConvertOpt) (*linter.Linter, error) {
216-
var lintOptionStr string
173+
var lintConfig *linter.Config
217174
if opt.Client != nil && opt.Client.LinterConfig != nil {
218-
lintOptionStr = *opt.Client.LinterConfig
175+
lintConfig = opt.Client.LinterConfig
219176
} else {
220-
lintOptionStr, _, _, _ = parser.ParseDirective("check", dt)
221-
}
222-
lintConfig, err := parseLintOptions(lintOptionStr)
223-
if err != nil {
224-
return nil, errors.Wrapf(err, "failed to parse check options")
177+
var err error
178+
lintOptionStr, _, _, _ := parser.ParseDirective("check", dt)
179+
lintConfig, err = linter.ParseLintOptions(lintOptionStr)
180+
if err != nil {
181+
return nil, errors.Wrapf(err, "failed to parse check options")
182+
}
225183
}
226184
lintConfig.Warn = opt.Warn
227185
return linter.New(lintConfig), nil

frontend/dockerfile/linter/linter.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package linter
22

33
import (
44
"fmt"
5+
"strconv"
56
"strings"
67

78
"github.com/moby/buildkit/frontend/dockerfile/parser"
@@ -100,3 +101,46 @@ func LintFormatShort(rulename, msg string, line int) string {
100101
}
101102

102103
type LintWarnFunc func(rulename, description, url, fmtmsg string, location []parser.Range)
104+
105+
func ParseLintOptions(checkStr string) (*Config, error) {
106+
checkStr = strings.TrimSpace(checkStr)
107+
if checkStr == "" {
108+
return &Config{}, nil
109+
}
110+
111+
parts := strings.SplitN(checkStr, ";", 2)
112+
var skipSet []string
113+
var errorOnWarn, skipAll bool
114+
for _, p := range parts {
115+
k, v, ok := strings.Cut(p, "=")
116+
if !ok {
117+
return nil, errors.Errorf("invalid check option %q", p)
118+
}
119+
k = strings.TrimSpace(k)
120+
switch k {
121+
case "skip":
122+
v = strings.TrimSpace(v)
123+
if v == "all" {
124+
skipAll = true
125+
} else {
126+
skipSet = strings.Split(v, ",")
127+
for i, rule := range skipSet {
128+
skipSet[i] = strings.TrimSpace(rule)
129+
}
130+
}
131+
case "error":
132+
v, err := strconv.ParseBool(strings.TrimSpace(v))
133+
if err != nil {
134+
return nil, errors.Wrapf(err, "failed to parse check option %q", p)
135+
}
136+
errorOnWarn = v
137+
default:
138+
return nil, errors.Errorf("invalid check option %q", k)
139+
}
140+
}
141+
return &Config{
142+
SkipRules: skipSet,
143+
SkipAll: skipAll,
144+
ReturnAsError: errorOnWarn,
145+
}, nil
146+
}

frontend/dockerui/config.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
controlapi "github.com/moby/buildkit/api/services/control"
1515
"github.com/moby/buildkit/client/llb"
1616
"github.com/moby/buildkit/frontend/attestations"
17+
"github.com/moby/buildkit/frontend/dockerfile/linter"
1718
"github.com/moby/buildkit/frontend/gateway/client"
1819
"github.com/moby/buildkit/solver/pb"
1920
"github.com/moby/buildkit/util/flightcontrol"
@@ -65,7 +66,7 @@ type Config struct {
6566
ShmSize int64
6667
Target string
6768
Ulimits []pb.Ulimit
68-
LinterConfig *string
69+
LinterConfig *linter.Config
6970

7071
CacheImports []client.CacheOptionsEntry
7172
TargetPlatforms []ocispecs.Platform // nil means default
@@ -280,7 +281,10 @@ func (bc *Client) init() error {
280281
bc.Hostname = opts[keyHostname]
281282

282283
if v, ok := opts[keyDockerfileLintArg]; ok {
283-
bc.LinterConfig = &v
284+
bc.LinterConfig, err = linter.ParseLintOptions(v)
285+
if err != nil {
286+
return errors.Wrapf(err, "failed to parse %s", keyDockerfileLintArg)
287+
}
284288
}
285289
return nil
286290
}

0 commit comments

Comments
 (0)