plancontext: use helpers to determine if CUE value is secret/fs/service

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-12-07 14:16:14 -05:00
parent 201ac391b4
commit c881bc2dba
7 changed files with 31 additions and 28 deletions

View File

@ -11,8 +11,8 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/client" "go.dagger.io/dagger/client"
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
"go.dagger.io/dagger/stdlib"
) )
func CurrentProject(ctx context.Context) *state.Project { 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 // FormatValue returns the String representation of the cue value
func FormatValue(val *compiler.Value) string { func FormatValue(val *compiler.Value) string {
if val.HasAttr("artifact") { switch {
case val.HasAttr("artifact"):
return "dagger.#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 { if val.IsConcreteR() != nil {
return val.IncompleteKind().String() return val.IncompleteKind().String()
} }

View File

@ -10,6 +10,7 @@ import (
"go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment" "go.dagger.io/dagger/environment"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver" "go.dagger.io/dagger/solver"
"go.dagger.io/dagger/state" "go.dagger.io/dagger/state"
@ -61,15 +62,15 @@ var listCmd = &cobra.Command{
_, hasDefault := inp.Default() _, hasDefault := inp.Default()
switch { switch {
case env.Context().Secrets.Contains(inp): case plancontext.IsSecretValue(inp):
if _, err := env.Context().Secrets.FromValue(inp); err != nil { if _, err := env.Context().Secrets.FromValue(inp); err != nil {
isConcrete = false isConcrete = false
} }
case env.Context().FS.Contains(inp): case plancontext.IsFSValue(inp):
if _, err := env.Context().FS.FromValue(inp); err != nil { if _, err := env.Context().FS.FromValue(inp); err != nil {
isConcrete = false isConcrete = false
} }
case env.Context().Services.Contains(inp): case plancontext.IsServiceValue(inp):
if _, err := env.Context().Services.FromValue(inp); err != nil { if _, err := env.Context().Services.FromValue(inp); err != nil {
isConcrete = false isConcrete = false
} }

View File

@ -13,6 +13,7 @@ import (
"go.dagger.io/dagger/compiler" "go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/environment" "go.dagger.io/dagger/environment"
"go.dagger.io/dagger/plan" "go.dagger.io/dagger/plan"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver" "go.dagger.io/dagger/solver"
"golang.org/x/term" "golang.org/x/term"
@ -142,15 +143,15 @@ func checkInputs(ctx context.Context, env *environment.Environment) error {
for _, i := range inputs { for _, i := range inputs {
isConcrete := (i.IsConcreteR(cue.Optional(true)) == nil) isConcrete := (i.IsConcreteR(cue.Optional(true)) == nil)
switch { switch {
case env.Context().Secrets.Contains(i): case plancontext.IsSecretValue(i):
if _, err := env.Context().Secrets.FromValue(i); err != nil { if _, err := env.Context().Secrets.FromValue(i); err != nil {
isConcrete = false isConcrete = false
} }
case env.Context().FS.Contains(i): case plancontext.IsFSValue(i):
if _, err := env.Context().FS.FromValue(i); err != nil { if _, err := env.Context().FS.FromValue(i); err != nil {
isConcrete = false isConcrete = false
} }
case env.Context().Services.Contains(i): case plancontext.IsServiceValue(i):
if _, err := env.Context().Services.FromValue(i); err != nil { if _, err := env.Context().Services.FromValue(i); err != nil {
isConcrete = false isConcrete = false
} }

View File

@ -100,7 +100,7 @@ func (p *Pipeline) ops() ([]*compiler.Value, error) {
// dagger.#FS forward compat // dagger.#FS forward compat
// FIXME: remove this // FIXME: remove this
if p.pctx.FS.Contains(p.code) { if plancontext.IsFSValue(p.code) {
ops = append(ops, 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) { func (p *Pipeline) doOp(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
// dagger.#FS forward compat // dagger.#FS forward compat
// FIXME: remove this // FIXME: remove this
if p.pctx.FS.Contains(op) { if plancontext.IsFSValue(op) {
fs, err := p.pctx.FS.FromValue(op) fs, err := p.pctx.FS.FromValue(op)
if err != nil { if err != nil {
return st, nil return st, nil

View File

@ -18,6 +18,10 @@ var (
) )
) )
func IsFSValue(v *compiler.Value) bool {
return v.LookupPath(fsIDPath).Exists()
}
type FS struct { type FS struct {
id string id string
result bkgw.Reference result bkgw.Reference
@ -54,10 +58,6 @@ func (c *fsContext) New(result bkgw.Reference) *FS {
return 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) { func (c *fsContext) FromValue(v *compiler.Value) (*FS, error) {
c.l.RLock() c.l.RLock()
defer c.l.RUnlock() defer c.l.RUnlock()

View File

@ -16,6 +16,10 @@ var (
) )
) )
func IsSecretValue(v *compiler.Value) bool {
return v.LookupPath(secretIDPath).Exists()
}
type Secret struct { type Secret struct {
id string id string
plainText string plainText string
@ -55,10 +59,6 @@ func (c *secretContext) New(plaintext string) *Secret {
return 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) { func (c *secretContext) FromValue(v *compiler.Value) (*Secret, error) {
c.l.RLock() c.l.RLock()
defer c.l.RUnlock() defer c.l.RUnlock()

View File

@ -16,6 +16,10 @@ var (
) )
) )
func IsServiceValue(v *compiler.Value) bool {
return v.LookupPath(serviceIDPath).Exists()
}
type Service struct { type Service struct {
id string id string
@ -62,10 +66,6 @@ func (c *serviceContext) New(unix, npipe string) *Service {
return s return s
} }
func (c *serviceContext) Contains(v *compiler.Value) bool {
return v.LookupPath(serviceIDPath).Exists()
}
func (c *serviceContext) FromValue(v *compiler.Value) (*Service, error) { func (c *serviceContext) FromValue(v *compiler.Value) (*Service, error) {
c.l.RLock() c.l.RLock()
defer c.l.RUnlock() defer c.l.RUnlock()