2021-05-26 01:30:49 +02:00
|
|
|
package environment
|
2021-05-19 23:50:40 +02:00
|
|
|
|
|
|
|
import (
|
2021-05-22 01:02:38 +02:00
|
|
|
"context"
|
2021-05-19 23:50:40 +02:00
|
|
|
|
2021-05-22 02:38:33 +02:00
|
|
|
"cuelang.org/go/cue"
|
2021-05-22 01:02:38 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-05-26 01:53:26 +02:00
|
|
|
"go.dagger.io/dagger/compiler"
|
2021-05-19 23:50:40 +02:00
|
|
|
)
|
|
|
|
|
2021-05-22 02:38:33 +02:00
|
|
|
func isReference(val cue.Value) bool {
|
|
|
|
isRef := func(v cue.Value) bool {
|
|
|
|
_, ref := v.ReferencePath()
|
2021-05-19 23:50:40 +02:00
|
|
|
|
2021-05-22 02:38:33 +02:00
|
|
|
if ref.String() == "" || v.Path().String() == ref.String() {
|
|
|
|
// not a reference
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range ref.Selectors() {
|
|
|
|
if s.IsDefinition() {
|
|
|
|
// if we reference to a definition, we skip the check
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2021-05-19 23:50:40 +02:00
|
|
|
}
|
|
|
|
|
2021-05-22 02:38:33 +02:00
|
|
|
op, vals := val.Expr()
|
|
|
|
if op == cue.NoOp {
|
|
|
|
return isRef(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range vals {
|
|
|
|
// if the expr has an op (& or |, etc...), check the expr values, recursively
|
|
|
|
if isReference(v) {
|
|
|
|
return true
|
2021-05-19 23:50:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 02:38:33 +02:00
|
|
|
return isRef(val)
|
2021-05-19 23:50:40 +02:00
|
|
|
}
|
|
|
|
|
2021-06-02 16:22:38 +02:00
|
|
|
func ScanInputs(ctx context.Context, value *compiler.Value) []*compiler.Value {
|
2021-05-22 01:02:38 +02:00
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
inputs := []*compiler.Value{}
|
2021-05-19 23:50:40 +02:00
|
|
|
|
|
|
|
value.Walk(
|
|
|
|
func(val *compiler.Value) bool {
|
2021-05-22 02:38:33 +02:00
|
|
|
if isReference(val.Cue()) {
|
2021-05-22 01:02:38 +02:00
|
|
|
lg.Debug().Str("value.Path", val.Path().String()).Msg("found reference, stop walk")
|
|
|
|
return false
|
|
|
|
}
|
2021-05-19 23:50:40 +02:00
|
|
|
|
2021-05-22 01:02:38 +02:00
|
|
|
if !val.HasAttr("input") {
|
|
|
|
return true
|
2021-05-19 23:50:40 +02:00
|
|
|
}
|
2021-05-22 01:02:38 +02:00
|
|
|
|
|
|
|
lg.Debug().Str("value.Path", val.Path().String()).Msg("found input")
|
|
|
|
inputs = append(inputs, val)
|
|
|
|
|
2021-05-19 23:50:40 +02:00
|
|
|
return true
|
|
|
|
}, nil,
|
|
|
|
)
|
|
|
|
|
2021-05-22 01:02:38 +02:00
|
|
|
return inputs
|
2021-05-19 23:50:40 +02:00
|
|
|
}
|
2021-05-31 15:24:42 +02:00
|
|
|
|
2021-06-02 16:22:38 +02:00
|
|
|
func ScanOutputs(ctx context.Context, value *compiler.Value) []*compiler.Value {
|
2021-05-31 15:24:42 +02:00
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
inputs := []*compiler.Value{}
|
|
|
|
|
|
|
|
value.Walk(
|
|
|
|
func(val *compiler.Value) bool {
|
|
|
|
if !val.HasAttr("output") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
lg.Debug().Str("value.Path", val.Path().String()).Msg("found output")
|
|
|
|
inputs = append(inputs, val)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
return inputs
|
|
|
|
}
|