Skip to content

Commit

Permalink
Remove Path field from SourceContext
Browse files Browse the repository at this point in the history
Also:
- update `processArgs` to do substitutions inline.
- update `fillDefaults` to set the default Source.Path to `"."` when the
  Source type is Context.

Signed-off-by: Peter Engelbert <pmengelbert@gmail.com>
  • Loading branch information
pmengelbert committed Jan 12, 2024
1 parent 1a864c1 commit d43ff4b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 50 deletions.
9 changes: 4 additions & 5 deletions docs/spec.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,13 @@
"SourceContext": {
"properties": {
"name": {
"type": "string"
},
"path": {
"type": "string"
"type": "string",
"description": "Name is the name of the build context. By default, it is the magic name\n`context`, recognized by Docker as the default context."
}
},
"additionalProperties": false,
"type": "object"
"type": "object",
"description": "SourceContext is used to generate a source from a build context."
},
"SourceDockerImage": {
"properties": {
Expand Down
68 changes: 30 additions & 38 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,64 +27,56 @@ const DefaultPatchStrip int = 1
func (s *Source) processArgs(args map[string]string) error {
lex := shell.NewLex('\\')

var sub *string
sub := func(s *string) error {
updated, err := lex.ProcessWordWithMap(*s, args)
if err != nil {
return err
}
*s = updated
return nil
}

switch {
case s.DockerImage != nil:
for _, mnt := range s.DockerImage.Cmd.Mounts {
if err := mnt.Spec.processArgs(args); err != nil {
return err
}
}
sub = &s.DockerImage.Ref
sub(&s.DockerImage.Ref)

Check failure on line 46 in load.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
case s.Git != nil:
sub = &s.Git.URL
fields := []*string{
&s.Git.URL,
&s.Git.Commit,
}
for _, f := range fields {
if err := sub(f); err != nil {
return err
}
}
case s.HTTPS != nil:
sub = &s.HTTPS.URL
sub(&s.HTTPS.URL)

Check failure on line 58 in load.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
case s.Context != nil:
updated, err := lex.ProcessWordWithMap(s.Context.Name, args)
if err != nil {
return err
}
s.Context.Name = updated

updated, err = lex.ProcessWordWithMap(s.Context.Path, args)
if err != nil {
if err := sub(&s.Context.Name); err != nil {
return err
}
s.Context.Path = updated

sub = nil
case s.Build != nil:
if err := s.Build.Source.processArgs(args); err != nil {
return err
}

updated, err := lex.ProcessWordWithMap(s.Build.DockerFile, args)
if err != nil {
return err
fields := []*string{
&s.Build.DockerFile,
&s.Build.Target,
}
s.Build.DockerFile = updated

updated, err = lex.ProcessWordWithMap(s.Build.Target, args)
if err != nil {
return err
for _, f := range fields {
if err := sub(f); err != nil {
return err
}
}
s.Build.Target = updated

sub = nil
default:
}

if sub == nil {
return nil
}

updated, err := lex.ProcessWordWithMap(*sub, args)
if err != nil {
return err
}

*sub = updated
return nil
}

Expand All @@ -100,8 +92,8 @@ func fillDefaults(s *Source) {
if s.Context.Name == "" {
s.Context.Name = dockerui.DefaultLocalNameContext
}
if s.Context.Path == "" {
s.Context.Path = "."
if s.Path == "" {
s.Path = "."
}
case s.Build != nil:
fillDefaults(&s.Build.Source)
Expand Down
6 changes: 0 additions & 6 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,12 @@ func source2LLBGetter(s *Spec, src Source, name string, forMount bool) LLBGetter
opts = append(opts, llb.Filename(name))
return llb.HTTP(https.URL, opts...), nil
case src.Context != nil:
srcCtx := src.Context

st, err := sOpt.GetContext(dockerui.DefaultLocalNameContext, localIncludeExcludeMerge(&src))
if err != nil {
return llb.Scratch(), err
}

includeExcludeHandled = true
if src.Path == "" && srcCtx.Path != "" {
src.Path = srcCtx.Path
}

return *st, nil
case src.Build != nil:
build := src.Build
Expand Down
5 changes: 4 additions & 1 deletion spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,12 @@ type SourceHTTPS struct {
URL string `yaml:"url" json:"url"`
}

// SourceContext is used to generate a source from a build context. The path to
// the build context is provided to the `Path` field of the owning `Source`.
type SourceContext struct {
// Name is the name of the build context. By default, it is the magic name
// `context`, recognized by Docker as the default context.
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Path string `yaml:"path,omitempty" json:"path,omitempty"`
}

// SourceBuild is used to generate source from a DockerFile build, either
Expand Down

0 comments on commit d43ff4b

Please sign in to comment.