diff --git a/environment/environment.go b/environment/environment.go index 7cb0a45a..a80300c2 100644 --- a/environment/environment.go +++ b/environment/environment.go @@ -2,6 +2,7 @@ package environment import ( "context" + "errors" "fmt" "io/fs" "strings" @@ -322,3 +323,25 @@ func (e *Environment) ScanInputs(ctx context.Context, mergeUserInputs bool) ([]* return scanInputs(ctx, src), nil } + +func (e *Environment) ScanOutputs(ctx context.Context) ([]*compiler.Value, error) { + if e.state.Computed == "" { + return nil, errors.New("cannot query environment outputs: please run `dagger up` first") + } + + src, err := e.prepare(ctx) + if err != nil { + return nil, err + } + + computed, err := compiler.DecodeJSON("", []byte(e.state.Computed)) + if err != nil { + return nil, err + } + + if err := src.FillPath(cue.MakePath(), computed); err != nil { + return nil, err + } + + return scanOutputs(ctx, src), nil +} diff --git a/environment/inputs_scan.go b/environment/inputs_scan.go index 9fcea5b0..0939a9ac 100644 --- a/environment/inputs_scan.go +++ b/environment/inputs_scan.go @@ -66,3 +66,23 @@ func scanInputs(ctx context.Context, value *compiler.Value) []*compiler.Value { return inputs } + +func scanOutputs(ctx context.Context, value *compiler.Value) []*compiler.Value { + 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 +}