@@ -4,10 +4,12 @@ import (
44 "context"
55 "fmt"
66 "io"
7+ "time"
78
89 "github.com/qri-io/dataset"
910 "github.com/qri-io/qri/base"
1011 "github.com/qri-io/qri/dsref"
12+ "github.com/qri-io/qri/event"
1113 "github.com/qri-io/qri/transform"
1214)
1315
@@ -30,12 +32,10 @@ func NewTransformMethods(inst *Instance) *TransformMethods {
3032type ApplyParams struct {
3133 Refstr string
3234 Transform * dataset.Transform
33- Source string
3435 Secrets map [string ]string
36+ Wait bool
3537
36- // TODO(dustmop): add `Wait bool`, if false, run the save asynchronously
37- // and return events on the bus that provide the progress of the save operation
38-
38+ Source string
3939 ScriptOutput io.Writer
4040}
4141
@@ -49,9 +49,8 @@ func (p *ApplyParams) Valid() error {
4949
5050// ApplyResult is the result of an apply command
5151type ApplyResult struct {
52- Data * dataset.Dataset
53- // TODO(dustmop): Make Apply asynchronous, running the transform in a go-routine.
54- // Return a channel that will send progress on the execution.
52+ Data * dataset.Dataset
53+ RunID string `json:"runID"`
5554}
5655
5756// Apply runs a transform script
@@ -87,15 +86,26 @@ func (m *TransformMethods) Apply(p *ApplyParams, res *ApplyResult) error {
8786 str := m .inst .node .LocalStreams
8887 loader := NewParseResolveLoadFunc ("" , m .inst .defaultResolver (), m .inst )
8988
89+ // allocate an ID for the transform, for now just log the events it produces
90+ runID := transform .NewRunID ()
91+ m .inst .bus .SubscribeID (func (ctx context.Context , e event.Event ) error {
92+ when := time .Unix (e .Timestamp / 1000000000 , e .Timestamp % 1000000000 )
93+ log .Infof ("[%s] event %s: %s" , when , e .Type , e .Payload )
94+ return nil
95+ }, runID )
96+
9097 scriptOut := p .ScriptOutput
91- err = transform .Apply (ctx , ds , loader , str , scriptOut , p .Secrets )
98+ err = transform .Apply (ctx , ds , loader , runID , m . inst . bus , p . Wait , str , scriptOut , p .Secrets )
9299 if err != nil {
93100 return err
94101 }
95102
96- if err = base .InlineJSONBody (ds ); err != nil && err != base .ErrNoBodyToInline {
97- return err
103+ if p .Wait {
104+ if err = base .InlineJSONBody (ds ); err != nil && err != base .ErrNoBodyToInline {
105+ return err
106+ }
107+ res .Data = ds
98108 }
99- res .Data = ds
109+ res .RunID = runID
100110 return nil
101111}
0 commit comments