github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/api/client/lib/image_push.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  // ImagePush request the docker host to push an image to 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) ImagePush(options types.ImagePushOptions, privilegeFunc RequestPrivilegeFunc) (io.ReadCloser, error) {
    16  	query := url.Values{}
    17  	query.Set("tag", options.Tag)
    18  
    19  	resp, err := cli.tryImagePush(options.ImageID, query, options.RegistryAuth)
    20  	if resp.statusCode == http.StatusUnauthorized {
    21  		newAuthHeader, privilegeErr := privilegeFunc()
    22  		if privilegeErr != nil {
    23  			return nil, privilegeErr
    24  		}
    25  		resp, err = cli.tryImagePush(options.ImageID, query, newAuthHeader)
    26  	}
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	return resp.body, nil
    31  }
    32  
    33  func (cli *Client) tryImagePush(imageID string, query url.Values, registryAuth string) (*serverResponse, error) {
    34  	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
    35  	return cli.post("/images/"+imageID+"/push", query, nil, headers)
    36  }