task_logs.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "context"
  4. "io"
  5. "net/url"
  6. "time"
  7. "github.com/docker/docker/api/types/container"
  8. timetypes "github.com/docker/docker/api/types/time"
  9. )
  10. // TaskLogs returns the logs generated by a task in an io.ReadCloser.
  11. // It's up to the caller to close the stream.
  12. func (cli *Client) TaskLogs(ctx context.Context, taskID string, options container.LogsOptions) (io.ReadCloser, error) {
  13. query := url.Values{}
  14. if options.ShowStdout {
  15. query.Set("stdout", "1")
  16. }
  17. if options.ShowStderr {
  18. query.Set("stderr", "1")
  19. }
  20. if options.Since != "" {
  21. ts, err := timetypes.GetTimestamp(options.Since, time.Now())
  22. if err != nil {
  23. return nil, err
  24. }
  25. query.Set("since", ts)
  26. }
  27. if options.Timestamps {
  28. query.Set("timestamps", "1")
  29. }
  30. if options.Details {
  31. query.Set("details", "1")
  32. }
  33. if options.Follow {
  34. query.Set("follow", "1")
  35. }
  36. query.Set("tail", options.Tail)
  37. resp, err := cli.get(ctx, "/tasks/"+taskID+"/logs", query, nil)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return resp.body, nil
  42. }