From 0768ac9f3f188f40b091d10e09b05185a29e690c Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 9 Dec 2021 15:20:56 -0500 Subject: [PATCH] compiler: FillFields helper, cleaned up repeated code Signed-off-by: Andrea Luzzardi --- compiler/value.go | 11 +++++++++++ plan/task/import.go | 9 +++------ plan/task/pull.go | 18 +++++------------- plan/task/secretenv.go | 9 +++------ plan/task/secretfile.go | 9 +++------ 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/compiler/value.go b/compiler/value.go index 30fdac18..50aed583 100644 --- a/compiler/value.go +++ b/compiler/value.go @@ -31,6 +31,17 @@ func (v *Value) FillPath(p cue.Path, x interface{}) error { 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 func (v *Value) LookupPath(p cue.Path) *Value { v.cc.rlock() diff --git a/plan/task/import.go b/plan/task/import.go index a567a223..30d09cd9 100644 --- a/plan/task/import.go +++ b/plan/task/import.go @@ -3,7 +3,6 @@ package task import ( "context" - "cuelang.org/go/cue" "github.com/moby/buildkit/client/llb" "go.dagger.io/dagger/compiler" "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) - out := compiler.NewValue() - if err := out.FillPath(cue.ParsePath("fs"), fs.MarshalCUE()); err != nil { - return nil, err - } - return out, nil + return compiler.NewValue().FillFields(map[string]interface{}{ + "fs": fs.MarshalCUE(), + }) } diff --git a/plan/task/pull.go b/plan/task/pull.go index d81e5241..2ff149b1 100644 --- a/plan/task/pull.go +++ b/plan/task/pull.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" - "cuelang.org/go/cue" "github.com/docker/distribution/reference" "github.com/moby/buildkit/client/llb" "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) - out := compiler.NewValue() - if err := out.FillPath(cue.ParsePath("output"), fs.MarshalCUE()); err != nil { - return nil, err - } - 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 + return compiler.NewValue().FillFields(map[string]interface{}{ + "output": fs.MarshalCUE(), + "digest": digest, + "config": image.Config, + }) } diff --git a/plan/task/secretenv.go b/plan/task/secretenv.go index f3db6923..1a67fa58 100644 --- a/plan/task/secretenv.go +++ b/plan/task/secretenv.go @@ -5,7 +5,6 @@ import ( "fmt" "os" - "cuelang.org/go/cue" "github.com/rs/zerolog/log" "go.dagger.io/dagger/compiler" "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) } secret := pctx.Secrets.New(env) - out := compiler.NewValue() - if err := out.FillPath(cue.ParsePath("contents"), secret.MarshalCUE()); err != nil { - return nil, err - } - return out, nil + return compiler.NewValue().FillFields(map[string]interface{}{ + "contents": secret.MarshalCUE(), + }) } diff --git a/plan/task/secretfile.go b/plan/task/secretfile.go index 3cb5a335..c41f25b3 100644 --- a/plan/task/secretfile.go +++ b/plan/task/secretfile.go @@ -4,7 +4,6 @@ import ( "context" "os" - "cuelang.org/go/cue" "github.com/rs/zerolog/log" "go.dagger.io/dagger/compiler" "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)) - out := compiler.NewValue() - if err := out.FillPath(cue.ParsePath("contents"), secret.MarshalCUE()); err != nil { - return nil, err - } - return out, nil + return compiler.NewValue().FillFields(map[string]interface{}{ + "contents": secret.MarshalCUE(), + }) }