Skip to content

Commit 0906879

Browse files
committed
Correctly handle reading from events channel
Signed-off-by: Michael Crosby <[email protected]>
1 parent 209a7fc commit 0906879

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

cmd/ctr/commands/events/events.go

+22-24
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,36 @@ var Command = cli.Command{
4242
defer cancel()
4343
eventsClient := client.EventService()
4444
eventsCh, errCh := eventsClient.Subscribe(ctx, context.Args()...)
45-
for {
45+
open := true
46+
for open {
4647
var e *events.Envelope
4748
select {
48-
case evt, closed := <-eventsCh:
49-
if closed {
50-
return nil
51-
}
52-
e = evt
53-
case err := <-errCh:
49+
case e = <-eventsCh:
50+
case err, open = <-errCh:
5451
return err
5552
}
56-
57-
var out []byte
58-
if e.Event != nil {
59-
v, err := typeurl.UnmarshalAny(e.Event)
60-
if err != nil {
61-
return err
53+
if e != nil {
54+
var out []byte
55+
if e.Event != nil {
56+
v, err := typeurl.UnmarshalAny(e.Event)
57+
if err != nil {
58+
return err
59+
}
60+
out, err = json.Marshal(v)
61+
if err != nil {
62+
return err
63+
}
6264
}
63-
out, err = json.Marshal(v)
64-
if err != nil {
65+
if _, err := fmt.Println(
66+
e.Timestamp,
67+
e.Namespace,
68+
e.Topic,
69+
string(out),
70+
); err != nil {
6571
return err
6672
}
6773
}
68-
69-
if _, err := fmt.Println(
70-
e.Timestamp,
71-
e.Namespace,
72-
e.Topic,
73-
string(out),
74-
); err != nil {
75-
return err
76-
}
7774
}
75+
return nil
7876
},
7977
}

0 commit comments

Comments
 (0)