image_list.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package lib
  2. import (
  3. "encoding/json"
  4. "net/url"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/pkg/parsers/filters"
  7. )
  8. // ImageListOptions holds parameters to filter the list of images with.
  9. type ImageListOptions struct {
  10. MatchName string
  11. All bool
  12. Filters filters.Args
  13. }
  14. // ImageList returns a list of images in the docker host.
  15. func (cli *Client) ImageList(options ImageListOptions) ([]types.Image, error) {
  16. var (
  17. images []types.Image
  18. query url.Values
  19. )
  20. if options.Filters.Len() > 0 {
  21. filterJSON, err := filters.ToParam(options.Filters)
  22. if err != nil {
  23. return images, err
  24. }
  25. query.Set("filters", filterJSON)
  26. }
  27. if options.MatchName != "" {
  28. // FIXME rename this parameter, to not be confused with the filters flag
  29. query.Set("filter", options.MatchName)
  30. }
  31. if options.All {
  32. query.Set("all", "1")
  33. }
  34. serverResp, err := cli.GET("/images/json?", query, nil)
  35. if err != nil {
  36. return images, err
  37. }
  38. defer ensureReaderClosed(serverResp)
  39. err = json.NewDecoder(serverResp.body).Decode(&images)
  40. return images, err
  41. }