diff options
Diffstat (limited to 'pkg/bindings')
-rw-r--r-- | pkg/bindings/system/system.go | 29 | ||||
-rw-r--r-- | pkg/bindings/test/system_test.go | 4 |
2 files changed, 21 insertions, 12 deletions
diff --git a/pkg/bindings/system/system.go b/pkg/bindings/system/system.go index 5348d0cfb..2cd894228 100644 --- a/pkg/bindings/system/system.go +++ b/pkg/bindings/system/system.go @@ -20,7 +20,7 @@ import ( // Events allows you to monitor libdpod related events like container creation and // removal. The events are then passed to the eventChan provided. The optional cancelChan // can be used to cancel the read of events and close down the HTTP connection. -func Events(ctx context.Context, eventChan chan (entities.Event), cancelChan chan bool, since, until *string, filters map[string][]string) error { +func Events(ctx context.Context, eventChan chan entities.Event, cancelChan chan bool, since, until *string, filters map[string][]string, stream *bool) error { conn, err := bindings.GetClient(ctx) if err != nil { return err @@ -32,6 +32,9 @@ func Events(ctx context.Context, eventChan chan (entities.Event), cancelChan cha if until != nil { params.Set("until", *until) } + if stream != nil { + params.Set("stream", strconv.FormatBool(*stream)) + } if filters != nil { filterString, err := bindings.FiltersToString(filters) if err != nil { @@ -50,18 +53,24 @@ func Events(ctx context.Context, eventChan chan (entities.Event), cancelChan cha logrus.Error(errors.Wrap(err, "unable to close event response body")) }() } + dec := json.NewDecoder(response.Body) - for { - e := entities.Event{} - if err := dec.Decode(&e); err != nil { - if err == io.EOF { - break - } - return errors.Wrap(err, "unable to decode event response") + for err = (error)(nil); err == nil; { + var e = entities.Event{} + err = dec.Decode(&e) + if err == nil { + eventChan <- e } - eventChan <- e } - return nil + close(eventChan) + switch { + case err == nil: + return nil + case errors.Is(err, io.EOF): + return nil + default: + return errors.Wrap(err, "unable to decode event response") + } } // Prune removes all unused system data. diff --git a/pkg/bindings/test/system_test.go b/pkg/bindings/test/system_test.go index 27ab2f555..dd3778754 100644 --- a/pkg/bindings/test/system_test.go +++ b/pkg/bindings/test/system_test.go @@ -47,13 +47,13 @@ var _ = Describe("Podman system", func() { } }() go func() { - system.Events(bt.conn, eChan, cancelChan, nil, nil, nil) + system.Events(bt.conn, eChan, cancelChan, nil, nil, nil, bindings.PFalse) }() _, err := bt.RunTopContainer(nil, nil, nil) Expect(err).To(BeNil()) cancelChan <- true - Expect(len(messages)).To(BeNumerically("==", 3)) + Expect(len(messages)).To(BeNumerically("==", 5)) }) It("podman system prune - pod,container stopped", func() { |