Skip to content

Commit

Permalink
Implement source.processArgs
Browse files Browse the repository at this point in the history
* Remove source.GetRef and source.SetRef
* Add source.processArgs and use it where necessary

Signed-off-by: Peter Engelbert <[email protected]>
  • Loading branch information
pmengelbert committed Dec 15, 2023
1 parent 0139795 commit 509d6e0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 55 deletions.
18 changes: 14 additions & 4 deletions frontend/rpm/handle_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,22 @@ func Dalec2SourcesLLB(spec *dalec.Spec, sOpt dalec.SourceOpts) ([]llb.State, err
return nil, err
}

ref, ok := src.GetRef()
if !ok {
continue
s := ""
switch {
case src.DockerImage != nil:
s = src.DockerImage.Ref
case src.Git != nil:
s = src.Git.URL
case src.HTTPS != nil:
s = src.HTTPS.URL
case src.Context != nil:
s = src.Context.Name
case src.Build != nil:
s = src.Build.Name
default:
}

pg := llb.ProgressGroup(pgID, "Add spec source: "+k+" "+ref, false)
pg := llb.ProgressGroup(pgID, "Add spec source: "+k+" "+s, false)
st, err := dalec.Source2LLBGetter(spec, src, k)(sOpt, pg)
if err != nil {
return nil, err
Expand Down
76 changes: 25 additions & 51 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,38 @@ func knownArg(key string) bool {
}
}

// GetRef determines the Source variant and returns the value of the `Ref`
// field, and the second return value will be `true`. If the variant doesn't
// have a `Ref` field, the second return value will be `false`.
func (s *Source) GetRef() (string, bool) {
func (s *Source) processArgs(args map[string]string) error {
lex := shell.NewLex('\\')

var sub *string
switch {
case s.DockerImage != nil:
return s.DockerImage.Ref, true
sub = &s.DockerImage.Ref
case s.Git != nil:
return s.Git.URL, true
sub = &s.Git.URL
case s.HTTPS != nil:
return s.HTTPS.URL, true
sub = &s.HTTPS.URL
case s.Context != nil:
return s.Context.Name, true
sub = &s.Context.Name
if *sub == "" {
*sub = "context"
}
case s.Build != nil:
return s.Build.Name, true
case s.Cmd != nil:
return "", false
sub = &s.Build.Name
default:
return "", false
}
}

// SetRef determines the Source variant and sets the `Ref` field. If the Source
// variant doesn't have a `Ref` field, SetRef does nothing and returns `false`.
func (s *Source) SetRef(ref string) bool {
switch {
case s.DockerImage != nil:
s.DockerImage.Ref = ref
return true
case s.Git != nil:
s.Git.URL = ref
return true
case s.HTTPS != nil:
s.HTTPS.URL = ref
return true
case s.Context != nil:
s.Context.Name = ref
return true
case s.Build != nil:
s.Build.Name = ref
return true
case s.Cmd != nil:
return false
default:
return false
if sub == nil {
return nil
}

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

*sub = updated
return nil
}

// LoadSpec loads a spec from the given data.
Expand All @@ -92,15 +78,9 @@ func LoadSpec(dt []byte, env map[string]string) (*Spec, error) {
}

for name, src := range spec.Sources {
ref, ok := src.GetRef()
if !ok {
continue
}
updated, err := lex.ProcessWordWithMap(ref, args)
if err != nil {
if err := src.processArgs(env, args); err != nil {

Check failure on line 81 in load.go

View workflow job for this annotation

GitHub Actions / integration

too many arguments in call to src.processArgs
return nil, fmt.Errorf("error performing shell expansion on source ref %q: %w", name, err)
}
_ = src.SetRef(updated)
if err := src.Cmd.processBuildArgs(lex, args, name); err != nil {
return nil, fmt.Errorf("error performing shell expansion on source %q: %w", name, err)
}
Expand Down Expand Up @@ -168,16 +148,10 @@ func (c *SourceCommand) processBuildArgs(lex *shell.Lex, args map[string]string,
if c == nil {
return nil
}
for i, smnt := range c.Mounts {
ref, ok := smnt.Spec.GetRef()
if !ok {
continue
}
updated, err := lex.ProcessWordWithMap(ref, args)
if err != nil {
for _, s := range c.Mounts {
if err := s.Spec.processArgs(nil, args); err != nil {

Check failure on line 152 in load.go

View workflow job for this annotation

GitHub Actions / integration

too many arguments in call to s.Spec.processArgs
return fmt.Errorf("error performing shell expansion on source ref %q: %w", name, err)
}
c.Mounts[i].Spec.SetRef(updated)
}
for k, v := range c.Env {
updated, err := lex.ProcessWordWithMap(v, args)
Expand Down

0 comments on commit 509d6e0

Please sign in to comment.