implemented ClientDoFunc callback to make the client.Up code reusable for other client actions

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-03-31 16:32:15 -07:00
parent 93a07db060
commit c583dc20ef
3 changed files with 18 additions and 14 deletions

View File

@ -61,8 +61,10 @@ func NewClient(ctx context.Context, host string) (*Client, error) {
}, nil }, nil
} }
type ClientDoFunc func(context.Context, *Deployment, Solver) error
// FIXME: return completed *Route, instead of *compiler.Value // 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) lg := log.Ctx(ctx)
eg, gctx := errgroup.WithContext(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() outr, outw := io.Pipe()
eg.Go(func() error { eg.Go(func() error {
defer outw.Close() defer outw.Close()
return c.buildfn(gctx, deployment, events, outw) return c.buildfn(gctx, deployment, fn, events, outw)
}) })
// Spawn output retriever // Spawn output retriever
@ -96,7 +98,7 @@ func (c *Client) Up(ctx context.Context, deployment *Deployment) (*compiler.Valu
return out, eg.Wait() 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) lg := log.Ctx(ctx)
// Scan local dirs to grant access // Scan local dirs to grant access
@ -138,10 +140,11 @@ func (c *Client) buildfn(ctx context.Context, deployment *Deployment, ch chan *b
} }
// Compute output overlay // Compute output overlay
lg.Debug().Msg("computing deployment") if fn != nil {
if err := deployment.Up(ctx, s, nil); err != nil { if err := fn(ctx, deployment, s); err != nil {
return nil, compiler.Err(err) return nil, compiler.Err(err)
} }
}
// Export deployment to a cue directory // Export deployment to a cue directory
// FIXME: this should be elsewhere // FIXME: this should be elsewhere
@ -149,9 +152,14 @@ func (c *Client) buildfn(ctx context.Context, deployment *Deployment, ch chan *b
span, _ := opentracing.StartSpanFromContext(ctx, "Deployment.Export") span, _ := opentracing.StartSpanFromContext(ctx, "Deployment.Export")
defer span.Finish() defer span.Finish()
stateSource, err := deployment.State().Source()
if err != nil {
return nil, compiler.Err(err)
}
st := llb.Scratch().File( st := llb.Scratch().File(
llb.Mkfile("state.cue", 0600, deployment.State().JSON()), llb.Mkfile("state.cue", 0600, stateSource),
llb.WithCustomName("[internal] serializing state to JSON"), llb.WithCustomName("[internal] serializing state to CUE"),
) )
ref, err := s.Solve(ctx, st) ref, err := s.Solve(ctx, st)
if err != nil { if err != nil {

View File

@ -204,11 +204,11 @@ func (v *Value) Validate() error {
} }
// Return cue source for this value // Return cue source for this value
func (v *Value) Source() ([]byte, error) { func (v *Value) Source(opts ...cue.Option) ([]byte, error) {
v.cc.rlock() v.cc.rlock()
defer v.cc.runlock() defer v.cc.runlock()
return cueformat.Node(v.val.Eval().Syntax()) return cueformat.Node(v.val.Eval().Syntax(opts...))
} }
func (v *Value) IsEmptyStruct() bool { func (v *Value) IsEmptyStruct() bool {

View File

@ -308,10 +308,6 @@ func (d *Deployment) Down(ctx context.Context, _ *DownOpts) error {
panic("NOT IMPLEMENTED") panic("NOT IMPLEMENTED")
} }
func (d *Deployment) Query(ctx context.Context, expr interface{}, o *QueryOpts) (*compiler.Value, error) {
panic("NOT IMPLEMENTED")
}
type QueryOpts struct{} type QueryOpts struct{}
func newTaskFunc(inst *cue.Instance, runner cueflow.RunnerFunc) cueflow.TaskFunc { func newTaskFunc(inst *cue.Instance, runner cueflow.RunnerFunc) cueflow.TaskFunc {