image_inspect.go 971 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "github.com/docker/engine-api/types"
  9. "golang.org/x/net/context"
  10. )
  11. // ImageInspectWithRaw returns the image information and it's raw representation.
  12. func (cli *Client) ImageInspectWithRaw(ctx context.Context, imageID string, getSize bool) (types.ImageInspect, []byte, error) {
  13. query := url.Values{}
  14. if getSize {
  15. query.Set("size", "1")
  16. }
  17. serverResp, err := cli.get(ctx, "/images/"+imageID+"/json", query, nil)
  18. if err != nil {
  19. if serverResp.statusCode == http.StatusNotFound {
  20. return types.ImageInspect{}, nil, imageNotFoundError{imageID}
  21. }
  22. return types.ImageInspect{}, nil, err
  23. }
  24. defer ensureReaderClosed(serverResp)
  25. body, err := ioutil.ReadAll(serverResp.body)
  26. if err != nil {
  27. return types.ImageInspect{}, nil, err
  28. }
  29. var response types.ImageInspect
  30. rdr := bytes.NewReader(body)
  31. err = json.NewDecoder(rdr).Decode(&response)
  32. return response, body, err
  33. }