diff --git a/client/container_create.go b/client/container_create.go index 9893579..bbef1c9 100644 --- a/client/container_create.go +++ b/client/container_create.go @@ -34,7 +34,7 @@ func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config serverResp, err := cli.post(ctx, "/containers/create", query, body, nil) if err != nil { - if serverResp != nil && serverResp.statusCode == 404 && strings.Contains(err.Error(), "No such image") { + if serverResp.statusCode == 404 && strings.Contains(err.Error(), "No such image") { return response, imageNotFoundError{config.Image} } return response, err diff --git a/client/image_create.go b/client/image_create.go index 6dfc039..1528b0b 100644 --- a/client/image_create.go +++ b/client/image_create.go @@ -28,7 +28,7 @@ func (cli *Client) ImageCreate(ctx context.Context, parentReference string, opti return resp.body, nil } -func (cli *Client) tryImageCreate(ctx context.Context, query url.Values, registryAuth string) (*serverResponse, error) { +func (cli *Client) tryImageCreate(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) { headers := map[string][]string{"X-Registry-Auth": {registryAuth}} return cli.post(ctx, "/images/create", query, nil, headers) } diff --git a/client/image_push.go b/client/image_push.go index 89191ee..a30fc3b 100644 --- a/client/image_push.go +++ b/client/image_push.go @@ -48,7 +48,7 @@ func (cli *Client) ImagePush(ctx context.Context, ref string, options types.Imag return resp.body, nil } -func (cli *Client) tryImagePush(ctx context.Context, imageID string, query url.Values, registryAuth string) (*serverResponse, error) { +func (cli *Client) tryImagePush(ctx context.Context, imageID string, query url.Values, registryAuth string) (serverResponse, error) { headers := map[string][]string{"X-Registry-Auth": {registryAuth}} return cli.post(ctx, "/images/"+imageID+"/push", query, nil, headers) } diff --git a/client/image_search.go b/client/image_search.go index 3940dfd..b7a7de3 100644 --- a/client/image_search.go +++ b/client/image_search.go @@ -45,7 +45,7 @@ func (cli *Client) ImageSearch(ctx context.Context, term string, options types.I return results, err } -func (cli *Client) tryImageSearch(ctx context.Context, query url.Values, registryAuth string) (*serverResponse, error) { +func (cli *Client) tryImageSearch(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) { headers := map[string][]string{"X-Registry-Auth": {registryAuth}} return cli.get(ctx, "/images/search", query, headers) } diff --git a/client/login.go b/client/login.go index 482f947..b14f239 100644 --- a/client/login.go +++ b/client/login.go @@ -14,7 +14,7 @@ import ( func (cli *Client) RegistryLogin(ctx context.Context, auth types.AuthConfig) (types.AuthResponse, error) { resp, err := cli.post(ctx, "/auth", url.Values{}, auth, nil) - if resp != nil && resp.statusCode == http.StatusUnauthorized { + if resp.statusCode == http.StatusUnauthorized { return types.AuthResponse{}, unauthorizedError{err} } if err != nil { diff --git a/client/plugin_install.go b/client/plugin_install.go index 3f5e59f..6cf50ff 100644 --- a/client/plugin_install.go +++ b/client/plugin_install.go @@ -53,7 +53,7 @@ func (cli *Client) PluginInstall(ctx context.Context, name string, options types return cli.PluginEnable(ctx, name) } -func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, registryAuth string) (*serverResponse, error) { +func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, registryAuth string) (serverResponse, error) { headers := map[string][]string{"X-Registry-Auth": {registryAuth}} return cli.post(ctx, "/plugins/pull", query, nil, headers) } diff --git a/client/request.go b/client/request.go index d6ad9f9..9cbb2b9 100644 --- a/client/request.go +++ b/client/request.go @@ -25,47 +25,47 @@ type serverResponse struct { } // head sends an http request to the docker API using the method HEAD. -func (cli *Client) head(ctx context.Context, path string, query url.Values, headers map[string][]string) (*serverResponse, error) { +func (cli *Client) head(ctx context.Context, path string, query url.Values, headers map[string][]string) (serverResponse, error) { return cli.sendRequest(ctx, "HEAD", path, query, nil, headers) } // getWithContext sends an http request to the docker API using the method GET with a specific go context. -func (cli *Client) get(ctx context.Context, path string, query url.Values, headers map[string][]string) (*serverResponse, error) { +func (cli *Client) get(ctx context.Context, path string, query url.Values, headers map[string][]string) (serverResponse, error) { return cli.sendRequest(ctx, "GET", path, query, nil, headers) } // postWithContext sends an http request to the docker API using the method POST with a specific go context. -func (cli *Client) post(ctx context.Context, path string, query url.Values, obj interface{}, headers map[string][]string) (*serverResponse, error) { +func (cli *Client) post(ctx context.Context, path string, query url.Values, obj interface{}, headers map[string][]string) (serverResponse, error) { return cli.sendRequest(ctx, "POST", path, query, obj, headers) } -func (cli *Client) postRaw(ctx context.Context, path string, query url.Values, body io.Reader, headers map[string][]string) (*serverResponse, error) { +func (cli *Client) postRaw(ctx context.Context, path string, query url.Values, body io.Reader, headers map[string][]string) (serverResponse, error) { return cli.sendClientRequest(ctx, "POST", path, query, body, headers) } // put sends an http request to the docker API using the method PUT. -func (cli *Client) put(ctx context.Context, path string, query url.Values, obj interface{}, headers map[string][]string) (*serverResponse, error) { +func (cli *Client) put(ctx context.Context, path string, query url.Values, obj interface{}, headers map[string][]string) (serverResponse, error) { return cli.sendRequest(ctx, "PUT", path, query, obj, headers) } // put sends an http request to the docker API using the method PUT. -func (cli *Client) putRaw(ctx context.Context, path string, query url.Values, body io.Reader, headers map[string][]string) (*serverResponse, error) { +func (cli *Client) putRaw(ctx context.Context, path string, query url.Values, body io.Reader, headers map[string][]string) (serverResponse, error) { return cli.sendClientRequest(ctx, "PUT", path, query, body, headers) } // delete sends an http request to the docker API using the method DELETE. -func (cli *Client) delete(ctx context.Context, path string, query url.Values, headers map[string][]string) (*serverResponse, error) { +func (cli *Client) delete(ctx context.Context, path string, query url.Values, headers map[string][]string) (serverResponse, error) { return cli.sendRequest(ctx, "DELETE", path, query, nil, headers) } -func (cli *Client) sendRequest(ctx context.Context, method, path string, query url.Values, obj interface{}, headers map[string][]string) (*serverResponse, error) { +func (cli *Client) sendRequest(ctx context.Context, method, path string, query url.Values, obj interface{}, headers map[string][]string) (serverResponse, error) { var body io.Reader if obj != nil { var err error body, err = encodeData(obj) if err != nil { - return nil, err + return serverResponse{}, err } if headers == nil { headers = make(map[string][]string) @@ -76,8 +76,8 @@ func (cli *Client) sendRequest(ctx context.Context, method, path string, query u return cli.sendClientRequest(ctx, method, path, query, body, headers) } -func (cli *Client) sendClientRequest(ctx context.Context, method, path string, query url.Values, body io.Reader, headers map[string][]string) (*serverResponse, error) { - serverResp := &serverResponse{ +func (cli *Client) sendClientRequest(ctx context.Context, method, path string, query url.Values, body io.Reader, headers map[string][]string) (serverResponse, error) { + serverResp := serverResponse{ body: nil, statusCode: -1, } @@ -199,10 +199,10 @@ func encodeData(data interface{}) (*bytes.Buffer, error) { return params, nil } -func ensureReaderClosed(response *serverResponse) { - if response != nil && response.body != nil { +func ensureReaderClosed(response serverResponse) { + if body := response.body; body != nil { // Drain up to 512 bytes and close the body to let the Transport reuse the connection - io.CopyN(ioutil.Discard, response.body, 512) + io.CopyN(ioutil.Discard, body, 512) response.body.Close() } }