events.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package client
  2. import (
  3. "io"
  4. "net/url"
  5. "time"
  6. "golang.org/x/net/context"
  7. "github.com/docker/engine-api/types"
  8. "github.com/docker/engine-api/types/filters"
  9. timetypes "github.com/docker/engine-api/types/time"
  10. )
  11. // Events returns a stream of events in the daemon in a ReadCloser.
  12. // It's up to the caller to close the stream.
  13. func (cli *Client) Events(ctx context.Context, options types.EventsOptions) (io.ReadCloser, error) {
  14. query := url.Values{}
  15. ref := time.Now()
  16. if options.Since != "" {
  17. ts, err := timetypes.GetTimestamp(options.Since, ref)
  18. if err != nil {
  19. return nil, err
  20. }
  21. query.Set("since", ts)
  22. }
  23. if options.Until != "" {
  24. ts, err := timetypes.GetTimestamp(options.Until, ref)
  25. if err != nil {
  26. return nil, err
  27. }
  28. query.Set("until", ts)
  29. }
  30. if options.Filters.Len() > 0 {
  31. filterJSON, err := filters.ToParam(options.Filters)
  32. if err != nil {
  33. return nil, err
  34. }
  35. query.Set("filters", filterJSON)
  36. }
  37. serverResponse, err := cli.get(ctx, "/events", query, nil)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return serverResponse.body, nil
  42. }