Skip to content

Commit

Permalink
Consolidate tests and update Warning output to include source data
Browse files Browse the repository at this point in the history
Signed-off-by: Talon Bowler <[email protected]>
  • Loading branch information
daghack committed Apr 10, 2024
1 parent 72d7aa7 commit 81fc6a3
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 187 deletions.
3 changes: 1 addition & 2 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
return dockerfile2llb.ListTargets(ctx, src.Data)
},
Lint: func(ctx context.Context) (*lint.LintResults, error) {
warnings, err := dockerfile2llb.DockerfileLint(ctx, src.Data, convertOpt)
return &lint.LintResults{Warnings: warnings}, err
return dockerfile2llb.DockerfileLint(ctx, src.Data, convertOpt)
},
}); err != nil {
return nil, err
Expand Down
39 changes: 23 additions & 16 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,39 @@ func Dockefile2Outline(ctx context.Context, dt []byte, opt ConvertOpt) (*outline
return &o, nil
}

func DockerfileLint(ctx context.Context, dt []byte, opt ConvertOpt) ([]lint.Warning, error) {
warnings := []lint.Warning{}
sourceData := strings.Split(string(opt.SourceMap.Data), "\n")
func DockerfileLint(ctx context.Context, dt []byte, opt ConvertOpt) (*lint.LintResults, error) {
results := &lint.LintResults{}
opt.Warn = func(short, url string, detail [][]byte, location []parser.Range) {
var rulename, ruledetail string
var ruleName, ruleDetail string
if strings.HasPrefix(short, "Lint Rule ") {
rulename = strings.TrimPrefix(short, "Lint Rule ")
ruleParts := strings.Split(rulename, ":")
rulename = strings.Trim(ruleParts[0], "'")
ruledetail = strings.TrimSpace(ruleParts[1])
ruleName = strings.TrimPrefix(short, "Lint Rule ")
ruleParts := strings.Split(ruleName, ":")
ruleName = strings.Trim(ruleParts[0], "'")
ruleDetail = strings.TrimSpace(ruleParts[1])
} else {
return
}
warnings = append(warnings, lint.Warning{
RuleName: rulename,
Detail: ruledetail,
Filename: opt.SourceMap.Filename,
Source: sourceData[location[0].Start.Line-1 : location[0].End.Line],
StartLine: location[0].Start.Line,
})
sourceIndex := results.AddSource(opt.SourceMap.Filename, "Dockerfile", opt.SourceMap.Data)
sourceLocation := []lint.Range{}
for _, loc := range location {
sourceLocation = append(sourceLocation, lint.Range{
Start: lint.Position{
Line: loc.Start.Line,
Column: loc.Start.Character,
},
End: lint.Position{
Line: loc.End.Line,
Column: loc.End.Character,
},
})
}
results.AddWarning(ruleName, ruleDetail, sourceIndex, sourceLocation)
}
_, err := toDispatchState(ctx, dt, opt)
if err != nil {
return nil, err
}
return warnings, nil
return results, nil
}

func ListTargets(ctx context.Context, dt []byte) (*targets.List, error) {
Expand Down
Loading

0 comments on commit 81fc6a3

Please sign in to comment.