Skip to content

Commit

Permalink
update print to include lint subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: Talon Bowler <[email protected]>
  • Loading branch information
daghack committed Apr 9, 2024
1 parent 647a997 commit 51b246b
Show file tree
Hide file tree
Showing 6 changed files with 587 additions and 340 deletions.
5 changes: 5 additions & 0 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/moby/buildkit/frontend/dockerui"
"github.com/moby/buildkit/frontend/gateway/client"
gwpb "github.com/moby/buildkit/frontend/gateway/pb"
"github.com/moby/buildkit/frontend/subrequests/lint"
"github.com/moby/buildkit/frontend/subrequests/outline"
"github.com/moby/buildkit/frontend/subrequests/targets"
"github.com/moby/buildkit/solver/errdefs"
Expand Down Expand Up @@ -85,6 +86,10 @@ func Build(ctx context.Context, c client.Client) (_ *client.Result, err error) {
ListTargets: func(ctx context.Context) (*targets.List, 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
},
}); err != nil {
return nil, err
} else if ok {
Expand Down
29 changes: 29 additions & 0 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/moby/buildkit/frontend/dockerfile/shell"
"github.com/moby/buildkit/frontend/dockerui"
"github.com/moby/buildkit/frontend/subrequests/lint"
"github.com/moby/buildkit/frontend/subrequests/outline"
"github.com/moby/buildkit/frontend/subrequests/targets"
"github.com/moby/buildkit/identity"
Expand Down Expand Up @@ -110,6 +111,34 @@ 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")
opt.Warn = func(short, url string, detail [][]byte, location []parser.Range) {
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])
} 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,
})
}
_, err := toDispatchState(ctx, dt, opt)
if err != nil {
return nil, err
}
return warnings, nil
}

func ListTargets(ctx context.Context, dt []byte) (*targets.List, error) {
dockerfile, err := parser.Parse(bytes.NewReader(dt))
if err != nil {
Expand Down
Loading

0 comments on commit 51b246b

Please sign in to comment.