From c583dc20ef0968314ece0069260fc38f7aef038e Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Wed, 31 Mar 2021 16:32:15 -0700 Subject: [PATCH] implemented ClientDoFunc callback to make the client.Up code reusable for other client actions Signed-off-by: Sam Alba --- dagger/client.go | 24 ++++++++++++++++-------- dagger/compiler/value.go | 4 ++-- dagger/deployment.go | 4 ---- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/dagger/client.go b/dagger/client.go index 22030f5a..318beb93 100644 --- a/dagger/client.go +++ b/dagger/client.go @@ -61,8 +61,10 @@ func NewClient(ctx context.Context, host string) (*Client, error) { }, nil } +type ClientDoFunc func(context.Context, *Deployment, Solver) error + // FIXME: return completed *Route, instead of *compiler.Value -func (c *Client) Up(ctx context.Context, deployment *Deployment) (*compiler.Value, error) { +func (c *Client) Do(ctx context.Context, deployment *Deployment, fn ClientDoFunc) (*compiler.Value, error) { lg := log.Ctx(ctx) eg, gctx := errgroup.WithContext(ctx) @@ -79,7 +81,7 @@ func (c *Client) Up(ctx context.Context, deployment *Deployment) (*compiler.Valu outr, outw := io.Pipe() eg.Go(func() error { defer outw.Close() - return c.buildfn(gctx, deployment, events, outw) + return c.buildfn(gctx, deployment, fn, events, outw) }) // Spawn output retriever @@ -96,7 +98,7 @@ func (c *Client) Up(ctx context.Context, deployment *Deployment) (*compiler.Valu return out, eg.Wait() } -func (c *Client) buildfn(ctx context.Context, deployment *Deployment, ch chan *bk.SolveStatus, w io.WriteCloser) error { +func (c *Client) buildfn(ctx context.Context, deployment *Deployment, fn ClientDoFunc, ch chan *bk.SolveStatus, w io.WriteCloser) error { lg := log.Ctx(ctx) // Scan local dirs to grant access @@ -138,9 +140,10 @@ func (c *Client) buildfn(ctx context.Context, deployment *Deployment, ch chan *b } // Compute output overlay - lg.Debug().Msg("computing deployment") - if err := deployment.Up(ctx, s, nil); err != nil { - return nil, compiler.Err(err) + if fn != nil { + if err := fn(ctx, deployment, s); err != nil { + return nil, compiler.Err(err) + } } // Export deployment to a cue directory @@ -149,9 +152,14 @@ func (c *Client) buildfn(ctx context.Context, deployment *Deployment, ch chan *b span, _ := opentracing.StartSpanFromContext(ctx, "Deployment.Export") defer span.Finish() + stateSource, err := deployment.State().Source() + if err != nil { + return nil, compiler.Err(err) + } + st := llb.Scratch().File( - llb.Mkfile("state.cue", 0600, deployment.State().JSON()), - llb.WithCustomName("[internal] serializing state to JSON"), + llb.Mkfile("state.cue", 0600, stateSource), + llb.WithCustomName("[internal] serializing state to CUE"), ) ref, err := s.Solve(ctx, st) if err != nil { diff --git a/dagger/compiler/value.go b/dagger/compiler/value.go index 2833134d..0dcaa1a6 100644 --- a/dagger/compiler/value.go +++ b/dagger/compiler/value.go @@ -204,11 +204,11 @@ func (v *Value) Validate() error { } // Return cue source for this value -func (v *Value) Source() ([]byte, error) { +func (v *Value) Source(opts ...cue.Option) ([]byte, error) { v.cc.rlock() defer v.cc.runlock() - return cueformat.Node(v.val.Eval().Syntax()) + return cueformat.Node(v.val.Eval().Syntax(opts...)) } func (v *Value) IsEmptyStruct() bool { diff --git a/dagger/deployment.go b/dagger/deployment.go index 8b0ad574..3726e49f 100644 --- a/dagger/deployment.go +++ b/dagger/deployment.go @@ -308,10 +308,6 @@ func (d *Deployment) Down(ctx context.Context, _ *DownOpts) error { panic("NOT IMPLEMENTED") } -func (d *Deployment) Query(ctx context.Context, expr interface{}, o *QueryOpts) (*compiler.Value, error) { - panic("NOT IMPLEMENTED") -} - type QueryOpts struct{} func newTaskFunc(inst *cue.Instance, runner cueflow.RunnerFunc) cueflow.TaskFunc {