Skip to content

Commit

Permalink
feat: print emit response output if it's a cloudevent (#444)
Browse files Browse the repository at this point in the history
The CloudEvent SDK will provide any event included in the response as a
return value from `Request()`. If it does, print it out. It makes the
experience nicer.

For example, if I run the default TypeScript event function locally, this
is what `kn emit...` looks like.

```
❯ ./func emit --sink local --data '{"hello": "world"}'
Context Attributes,
  specversion: 1.0
  type: echo
  source: function.eventViewer
  id: d7d81ccc-a365-4433-be6b-7edfa43ca360
  time: 2021-07-27T18:57:03.147Z
  datacontenttype: application/json; charset=utf-8
Data,
  {
    "hello": "world"
  }
```

Signed-off-by: Lance Ball <lball@redhat.com>
  • Loading branch information
lance committed Jul 28, 2021
1 parent 1a1288a commit a25b723
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cloudevents/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ func (e *Emitter) Emit(ctx context.Context, endpoint string) (err error) {
if err = evt.SetData(e.ContentType, e.Data); err != nil {
return
}
if result := c.Send(ctx, evt); cloudevents.IsUndelivered(result) {
event, result := c.Request(ctx, evt)
if !cloudevents.IsACK(result) {
return fmt.Errorf(result.Error())
}
if event != nil {
fmt.Printf("%v", event)
}
return nil
}

Expand Down

0 comments on commit a25b723

Please sign in to comment.