Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions dockerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,25 @@ func NewDockerClientTimeout(daemonUrl string, tlsConfig *tls.Config, timeout tim

func (client *DockerClient) doRequest(method string, path string, body []byte, headers map[string]string) ([]byte, error) {
b := bytes.NewBuffer(body)
req, err := http.NewRequest(method, client.URL.String()+path, b)

reader, err := client.doStreamRequest(method, path, b, headers)
if err != nil {
return nil, err
}

defer reader.Close()
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
return data, nil
}

func (client *DockerClient) doStreamRequest(method string, path string, in io.Reader, headers map[string]string) (io.ReadCloser, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between this and doRequest? Can the two be refactored so they share most of the same code?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doStreamRequest: the input and output is using io.Reader and io.Writer. it can work streamly.
doRequest: the input and output is byte[]. if we send a request, we have to wait all the response. it can not work streamly.

if (method == "POST" || method == "PUT") && in == nil {
in = bytes.NewReader(nil)
}
req, err := http.NewRequest(method, client.URL.String()+path, in)
if err != nil {
return nil, err
}
Expand All @@ -83,18 +101,19 @@ func (client *DockerClient) doRequest(method string, path string, body []byte, h
}
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode == 404 {
return nil, ErrNotFound
}
if resp.StatusCode >= 400 {
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, Error{StatusCode: resp.StatusCode, Status: resp.Status, msg: string(data)}
}
return data, nil

return resp.Body, nil
}

func (client *DockerClient) Info() (*Info, error) {
Expand Down Expand Up @@ -464,3 +483,25 @@ func (client *DockerClient) RenameContainer(oldName string, newName string) erro
_, err := client.doRequest("POST", uri, nil, nil)
return err
}

func (client *DockerClient) ImportImage(source string, repository string, tag string, tar io.Reader) (io.ReadCloser, error) {
var fromSrc string
v := &url.Values{}
if source == "" {
fromSrc = "-"
} else {
fromSrc = source
}

v.Set("fromSrc", fromSrc)
v.Set("repo", repository)
if tag != "" {
v.Set("tag", tag)
}

var in io.Reader
if fromSrc == "-" {
in = tar
}
return client.doStreamRequest("POST", "/images/create?"+v.Encode(), in, nil)
}
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ type Client interface {
PauseContainer(name string) error
UnpauseContainer(name string) error
RenameContainer(oldName string, newName string) error
ImportImage(source string, repository string, tag string, tar io.Reader) (io.ReadCloser, error)
}
5 changes: 5 additions & 0 deletions mockclient/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,8 @@ func (client *MockClient) RenameContainer(oldName string, newName string) error
args := client.Mock.Called(oldName, newName)
return args.Error(0)
}

func (client *MockClient) ImportImage(source string, repository string, tag string, tar io.Reader) (io.ReadCloser, error) {
args := client.Mock.Called(source, repository, tag, tar)
return args.Get(0).(io.ReadCloser), args.Error(1)
}