compiler: FillFields helper, cleaned up repeated code

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-12-09 15:20:56 -05:00
parent ff6c7d1c1f
commit 0768ac9f3f
5 changed files with 25 additions and 31 deletions

View File

@ -31,6 +31,17 @@ func (v *Value) FillPath(p cue.Path, x interface{}) error {
return v.val.Err() return v.val.Err()
} }
// FillFields fills multiple fields, in place
func (v *Value) FillFields(values map[string]interface{}) (*Value, error) {
for p, x := range values {
if err := v.FillPath(cue.ParsePath(p), x); err != nil {
return nil, err
}
}
return v, nil
}
// LookupPath is a concurrency safe wrapper around cue.Value.LookupPath // LookupPath is a concurrency safe wrapper around cue.Value.LookupPath
func (v *Value) LookupPath(p cue.Path) *Value { func (v *Value) LookupPath(p cue.Path) *Value {
v.cc.rlock() v.cc.rlock()

View File

@ -3,7 +3,6 @@ package task
import ( import (
"context" "context"
"cuelang.org/go/cue"
"github.com/moby/buildkit/client/llb" "github.com/moby/buildkit/client/llb"
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext" "go.dagger.io/dagger/plancontext"
@ -69,9 +68,7 @@ func (c importTask) Run(ctx context.Context, pctx *plancontext.Context, s solver
} }
fs := pctx.FS.New(result) fs := pctx.FS.New(result)
out := compiler.NewValue() return compiler.NewValue().FillFields(map[string]interface{}{
if err := out.FillPath(cue.ParsePath("fs"), fs.MarshalCUE()); err != nil { "fs": fs.MarshalCUE(),
return nil, err })
}
return out, nil
} }

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"cuelang.org/go/cue"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
"github.com/moby/buildkit/client/llb" "github.com/moby/buildkit/client/llb"
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
@ -64,16 +63,9 @@ func (c *pullTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.
} }
fs := pctx.FS.New(result) fs := pctx.FS.New(result)
out := compiler.NewValue() return compiler.NewValue().FillFields(map[string]interface{}{
if err := out.FillPath(cue.ParsePath("output"), fs.MarshalCUE()); err != nil { "output": fs.MarshalCUE(),
return nil, err "digest": digest,
} "config": image.Config,
if err := out.FillPath(cue.ParsePath("digest"), digest.String()); err != nil { })
return nil, err
}
if err := out.FillPath(cue.ParsePath("config"), image.Config); err != nil {
return nil, err
}
return out, nil
} }

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"os" "os"
"cuelang.org/go/cue"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext" "go.dagger.io/dagger/plancontext"
@ -37,9 +36,7 @@ func (c secretEnvTask) Run(ctx context.Context, pctx *plancontext.Context, _ sol
return nil, fmt.Errorf("environment variable %q not set", secretEnv.Envvar) return nil, fmt.Errorf("environment variable %q not set", secretEnv.Envvar)
} }
secret := pctx.Secrets.New(env) secret := pctx.Secrets.New(env)
out := compiler.NewValue() return compiler.NewValue().FillFields(map[string]interface{}{
if err := out.FillPath(cue.ParsePath("contents"), secret.MarshalCUE()); err != nil { "contents": secret.MarshalCUE(),
return nil, err })
}
return out, nil
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"os" "os"
"cuelang.org/go/cue"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext" "go.dagger.io/dagger/plancontext"
@ -37,9 +36,7 @@ func (c secretFileTask) Run(ctx context.Context, pctx *plancontext.Context, _ so
} }
secret := pctx.Secrets.New(string(plaintext)) secret := pctx.Secrets.New(string(plaintext))
out := compiler.NewValue() return compiler.NewValue().FillFields(map[string]interface{}{
if err := out.FillPath(cue.ParsePath("contents"), secret.MarshalCUE()); err != nil { "contents": secret.MarshalCUE(),
return nil, err })
}
return out, nil
} }