github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/api/client/lib/image_pull.go (about)

     1  package lib
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/api/types"
     9  )
    10  
    11  // ImagePull request the docker host to pull an image from a remote registry.
    12  // It executes the privileged function if the operation is unauthorized
    13  // and it tries one more time.
    14  // It's up to the caller to handle the io.ReadCloser and close it properly.
    15  func (cli *Client) ImagePull(options types.ImagePullOptions, privilegeFunc RequestPrivilegeFunc) (io.ReadCloser, error) {
    16  	query := url.Values{}
    17  	query.Set("fromImage", options.ImageID)
    18  	if options.Tag != "" {
    19  		query.Set("tag", options.Tag)
    20  	}
    21  
    22  	resp, err := cli.tryImageCreate(query, options.RegistryAuth)
    23  	if resp.statusCode == http.StatusUnauthorized {
    24  		newAuthHeader, privilegeErr := privilegeFunc()
    25  		if privilegeErr != nil {
    26  			return nil, privilegeErr
    27  		}
    28  		resp, err = cli.tryImageCreate(query, newAuthHeader)
    29  	}
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	return resp.body, nil
    34  }