From c881bc2dba1e73115667e3f0223dec189c05b904 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Tue, 7 Dec 2021 14:16:14 -0500 Subject: [PATCH] plancontext: use helpers to determine if CUE value is secret/fs/service Signed-off-by: Andrea Luzzardi --- cmd/dagger/cmd/common/common.go | 17 +++++++++-------- cmd/dagger/cmd/input/list.go | 7 ++++--- cmd/dagger/cmd/up.go | 7 ++++--- environment/pipeline.go | 4 ++-- plancontext/fs.go | 8 ++++---- plancontext/secret.go | 8 ++++---- plancontext/service.go | 8 ++++---- 7 files changed, 31 insertions(+), 28 deletions(-) diff --git a/cmd/dagger/cmd/common/common.go b/cmd/dagger/cmd/common/common.go index 71929ccd..295b20ae 100644 --- a/cmd/dagger/cmd/common/common.go +++ b/cmd/dagger/cmd/common/common.go @@ -11,8 +11,8 @@ import ( "github.com/spf13/viper" "go.dagger.io/dagger/client" "go.dagger.io/dagger/compiler" + "go.dagger.io/dagger/plancontext" "go.dagger.io/dagger/state" - "go.dagger.io/dagger/stdlib" ) func CurrentProject(ctx context.Context) *state.Project { @@ -86,16 +86,17 @@ func CurrentEnvironmentState(ctx context.Context, project *state.Project) *state // FormatValue returns the String representation of the cue value func FormatValue(val *compiler.Value) string { - if val.HasAttr("artifact") { + switch { + case val.HasAttr("artifact"): return "dagger.#Artifact" + case plancontext.IsSecretValue(val): + return "dagger.#Secret" + case plancontext.IsFSValue(val): + return "dagger.#FS" + case plancontext.IsServiceValue(val): + return "dagger.#Service" } - if val.LookupPath(cue.MakePath( - cue.Hid("_secret", stdlib.PackageName), - cue.Str("id"), - )).Exists() { - return "dagger.#Secret" - } if val.IsConcreteR() != nil { return val.IncompleteKind().String() } diff --git a/cmd/dagger/cmd/input/list.go b/cmd/dagger/cmd/input/list.go index dd7cc07d..8589db02 100644 --- a/cmd/dagger/cmd/input/list.go +++ b/cmd/dagger/cmd/input/list.go @@ -10,6 +10,7 @@ import ( "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/compiler" "go.dagger.io/dagger/environment" + "go.dagger.io/dagger/plancontext" "go.dagger.io/dagger/solver" "go.dagger.io/dagger/state" @@ -61,15 +62,15 @@ var listCmd = &cobra.Command{ _, hasDefault := inp.Default() switch { - case env.Context().Secrets.Contains(inp): + case plancontext.IsSecretValue(inp): if _, err := env.Context().Secrets.FromValue(inp); err != nil { isConcrete = false } - case env.Context().FS.Contains(inp): + case plancontext.IsFSValue(inp): if _, err := env.Context().FS.FromValue(inp); err != nil { isConcrete = false } - case env.Context().Services.Contains(inp): + case plancontext.IsServiceValue(inp): if _, err := env.Context().Services.FromValue(inp); err != nil { isConcrete = false } diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index 1dc859b7..3e6960cd 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -13,6 +13,7 @@ import ( "go.dagger.io/dagger/compiler" "go.dagger.io/dagger/environment" "go.dagger.io/dagger/plan" + "go.dagger.io/dagger/plancontext" "go.dagger.io/dagger/solver" "golang.org/x/term" @@ -142,15 +143,15 @@ func checkInputs(ctx context.Context, env *environment.Environment) error { for _, i := range inputs { isConcrete := (i.IsConcreteR(cue.Optional(true)) == nil) switch { - case env.Context().Secrets.Contains(i): + case plancontext.IsSecretValue(i): if _, err := env.Context().Secrets.FromValue(i); err != nil { isConcrete = false } - case env.Context().FS.Contains(i): + case plancontext.IsFSValue(i): if _, err := env.Context().FS.FromValue(i); err != nil { isConcrete = false } - case env.Context().Services.Contains(i): + case plancontext.IsServiceValue(i): if _, err := env.Context().Services.FromValue(i); err != nil { isConcrete = false } diff --git a/environment/pipeline.go b/environment/pipeline.go index 76beef79..31b8e8e3 100644 --- a/environment/pipeline.go +++ b/environment/pipeline.go @@ -100,7 +100,7 @@ func (p *Pipeline) ops() ([]*compiler.Value, error) { // dagger.#FS forward compat // FIXME: remove this - if p.pctx.FS.Contains(p.code) { + if plancontext.IsFSValue(p.code) { ops = append(ops, p.code) } @@ -212,7 +212,7 @@ func (p *Pipeline) run(ctx context.Context) error { func (p *Pipeline) doOp(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) { // dagger.#FS forward compat // FIXME: remove this - if p.pctx.FS.Contains(op) { + if plancontext.IsFSValue(op) { fs, err := p.pctx.FS.FromValue(op) if err != nil { return st, nil diff --git a/plancontext/fs.go b/plancontext/fs.go index 208a9827..dc8ca6a2 100644 --- a/plancontext/fs.go +++ b/plancontext/fs.go @@ -18,6 +18,10 @@ var ( ) ) +func IsFSValue(v *compiler.Value) bool { + return v.LookupPath(fsIDPath).Exists() +} + type FS struct { id string result bkgw.Reference @@ -54,10 +58,6 @@ func (c *fsContext) New(result bkgw.Reference) *FS { return fs } -func (c *fsContext) Contains(v *compiler.Value) bool { - return v.LookupPath(fsIDPath).Exists() -} - func (c *fsContext) FromValue(v *compiler.Value) (*FS, error) { c.l.RLock() defer c.l.RUnlock() diff --git a/plancontext/secret.go b/plancontext/secret.go index 8ff97dd1..0a08ab67 100644 --- a/plancontext/secret.go +++ b/plancontext/secret.go @@ -16,6 +16,10 @@ var ( ) ) +func IsSecretValue(v *compiler.Value) bool { + return v.LookupPath(secretIDPath).Exists() +} + type Secret struct { id string plainText string @@ -55,10 +59,6 @@ func (c *secretContext) New(plaintext string) *Secret { return secret } -func (c *secretContext) Contains(v *compiler.Value) bool { - return v.LookupPath(secretIDPath).Exists() -} - func (c *secretContext) FromValue(v *compiler.Value) (*Secret, error) { c.l.RLock() defer c.l.RUnlock() diff --git a/plancontext/service.go b/plancontext/service.go index 4b4c7f9c..0b2a7bee 100644 --- a/plancontext/service.go +++ b/plancontext/service.go @@ -16,6 +16,10 @@ var ( ) ) +func IsServiceValue(v *compiler.Value) bool { + return v.LookupPath(serviceIDPath).Exists() +} + type Service struct { id string @@ -62,10 +66,6 @@ func (c *serviceContext) New(unix, npipe string) *Service { return s } -func (c *serviceContext) Contains(v *compiler.Value) bool { - return v.LookupPath(serviceIDPath).Exists() -} - func (c *serviceContext) FromValue(v *compiler.Value) (*Service, error) { c.l.RLock() defer c.l.RUnlock()