container_list.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package lib
  2. import (
  3. "encoding/json"
  4. "net/url"
  5. "strconv"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/pkg/parsers/filters"
  8. )
  9. // ContainerListOptions holds parameters to list containers with.
  10. type ContainerListOptions struct {
  11. Quiet bool
  12. Size bool
  13. All bool
  14. Latest bool
  15. Since string
  16. Before string
  17. Limit int
  18. Filter filters.Args
  19. }
  20. // ContainerList returns the list of containers in the docker host.
  21. func (cli *Client) ContainerList(options ContainerListOptions) ([]types.Container, error) {
  22. var query url.Values
  23. if options.All {
  24. query.Set("all", "1")
  25. }
  26. if options.Limit != -1 {
  27. query.Set("limit", strconv.Itoa(options.Limit))
  28. }
  29. if options.Since != "" {
  30. query.Set("since", options.Since)
  31. }
  32. if options.Before != "" {
  33. query.Set("before", options.Before)
  34. }
  35. if options.Size {
  36. query.Set("size", "1")
  37. }
  38. if options.Filter.Len() > 0 {
  39. filterJSON, err := filters.ToParam(options.Filter)
  40. if err != nil {
  41. return nil, err
  42. }
  43. query.Set("filters", filterJSON)
  44. }
  45. resp, err := cli.GET("/containers/json", query, nil)
  46. if err != nil {
  47. return nil, err
  48. }
  49. defer ensureReaderClosed(resp)
  50. var containers []types.Container
  51. err = json.NewDecoder(resp.body).Decode(&containers)
  52. return containers, err
  53. }