added support for output scanning

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-05-31 15:24:42 +02:00
parent 21b259fc86
commit 6e3ec02ceb
2 changed files with 43 additions and 0 deletions

View File

@ -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
}

View File

@ -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
}