Skip to content

Commit

Permalink
update lint tests to accept a struct instead of an increasing number …
Browse files Browse the repository at this point in the history
…of parameters

Signed-off-by: Talon Bowler <[email protected]>
  • Loading branch information
daghack committed Apr 25, 2024
1 parent 63bae6e commit 1c07614
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 144 deletions.
49 changes: 23 additions & 26 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
if v, ok := opt.BuildArgs[metaArg.Key]; !ok {
if metaArg.Value != nil {
result, _ := shlex.ProcessWordWithMatches(*metaArg.Value, metaArgsToMap(optMetaArgs))
*metaArg.Value = result.Word
*metaArg.Value = result.Result
info.deps = result.Matched
}
} else {
Expand All @@ -250,22 +250,17 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS

// set base state for every image
for i, st := range stages {
result, err := shlex.ProcessWordWithMatches(st.BaseName, metaArgsToMap(optMetaArgs))
name := result.Word
used := result.Matched
if len(result.Unmatched) > 0 {
for unmatched := range result.Unmatched {
msg := linter.RuleUndeclaredArgInFrom.Format(unmatched)
linter.RuleUndeclaredArgInFrom.Run(opt.Warn, st.Location, msg)
}
}
nameMatch, err := shlex.ProcessWordWithMatches(st.BaseName, metaArgsToMap(optMetaArgs))
reportUnusedFromArgs(nameMatch.Unmatched, st.Location, opt.Warn)
used := nameMatch.Matched

if err != nil {
return nil, parser.WithLocation(err, st.Location)
}
if name == "" {
if nameMatch.Result == "" {
return nil, parser.WithLocation(errors.Errorf("base name (%s) should not be blank", st.BaseName), st.Location)
}
st.BaseName = name
st.BaseName = nameMatch.Result

ds := &dispatchState{
stage: st,
Expand All @@ -279,27 +274,22 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS
}

if v := st.Platform; v != "" {
result, err := shlex.ProcessWordWithMatches(v, metaArgsToMap(optMetaArgs))
v := result.Word
u := result.Matched

if len(result.Unmatched) > 0 {
for unmatched := range result.Unmatched {
msg := linter.RuleUndeclaredArgInFrom.Format(unmatched)
linter.RuleUndeclaredArgInFrom.Run(opt.Warn, st.Location, msg)
}
}
platMatch, err := shlex.ProcessWordWithMatches(v, metaArgsToMap(optMetaArgs))
reportUnusedFromArgs(platMatch.Unmatched, st.Location, opt.Warn)

if err != nil {
return nil, parser.WithLocation(errors.Wrapf(err, "failed to process arguments for platform %s", v), st.Location)
return nil, parser.WithLocation(errors.Wrapf(err, "failed to process arguments for platform %s", platMatch.Result), st.Location)
}

p, err := platforms.Parse(v)
p, err := platforms.Parse(platMatch.Result)
if err != nil {
return nil, parser.WithLocation(errors.Wrapf(err, "failed to parse platform %s", v), st.Location)
return nil, parser.WithLocation(errors.Wrapf(err, "failed to parse platform %s", platMatch.Result), st.Location)
}
for k := range u {

for k := range platMatch.Matched {
used[k] = struct{}{}
}

ds.platform = &p
}

Expand Down Expand Up @@ -2010,3 +2000,10 @@ func validateCommandCasing(dockerfile *parser.Result, warn linter.LintWarnFunc)
}
}
}

func reportUnusedFromArgs(unmatched map[string]struct{}, location []parser.Range, warn linter.LintWarnFunc) {
for arg := range unmatched {
msg := linter.RuleUndeclaredArgInFrom.Format(arg)
linter.RuleUndeclaredArgInFrom.Run(warn, location, msg)
}
}
Loading

0 comments on commit 1c07614

Please sign in to comment.