Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

add cliOut param to PullImage #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 32 additions & 9 deletions dockerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"strings"
"sync/atomic"
"time"

"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
)

const (
Expand Down Expand Up @@ -441,29 +444,49 @@ func (client *DockerClient) Version() (*Version, error) {
return version, nil
}

func (client *DockerClient) PullImage(name string, auth *AuthConfig) error {
func (client *DockerClient) PullImage(name string, auth *AuthConfig, cliOut io.Writer) (err error) {
v := url.Values{}
v.Set("fromImage", name)
uri := fmt.Sprintf("/%s/images/create?%s", APIVersion, v.Encode())
req, err := http.NewRequest("POST", client.URL.String()+uri, nil)
req, _ := http.NewRequest("POST", client.URL.String()+uri, nil)
if auth != nil {
req.Header.Add("X-Registry-Auth", auth.encode())
}
resp, err := client.HTTPClient.Do(req)
var resp *http.Response
resp, err = client.HTTPClient.Do(req)
if err != nil {
return err
return
}
defer resp.Body.Close()
errorReader := io.Reader(resp.Body)
if cliOut != nil {
pipeReader, pipeWriter := io.Pipe()
streamErrChan := make(chan error)
defer func() {
pipeWriter.Close()
if err == nil {
err = <-streamErrChan
}
}()
errorReader = io.TeeReader(resp.Body, pipeWriter)
go func() {
fd, isTerminalIn := term.GetFdInfo(cliOut)
streamErrChan <- jsonmessage.DisplayJSONMessagesStream(pipeReader, cliOut, fd, isTerminalIn)
}()
}
var finalObj map[string]interface{}
for decoder := json.NewDecoder(resp.Body); err == nil; err = decoder.Decode(&finalObj) {
for decoder := json.NewDecoder(errorReader); err == nil; err = decoder.Decode(&finalObj) {
}
if err != io.EOF {
return err
return
} else {
err = nil
}
if err, ok := finalObj["error"]; ok {
return fmt.Errorf("%v", err)
if errObj, ok := finalObj["error"]; ok {
err = fmt.Errorf("%v", errObj)
return
}
return nil
return
}

func (client *DockerClient) InspectImage(id string) (*ImageInfo, error) {
Expand Down
21 changes: 16 additions & 5 deletions dockerclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"strings"
"testing"
Expand All @@ -30,6 +31,16 @@ func testDockerClient(t *testing.T) *DockerClient {
return client
}

func ExampleDockerClient_PullImage() {
docker, err := NewDockerClient("unix:///var/run/docker.sock", nil)
if err != nil {
panic(err)
}
if err := docker.PullImage("busybox", nil, os.Stdout); err != nil {
panic(err)
}
}

func TestInfo(t *testing.T) {
client := testDockerClient(t)
info, err := client.Info()
Expand All @@ -49,17 +60,17 @@ func TestKillContainer(t *testing.T) {

func TestPullImage(t *testing.T) {
client := testDockerClient(t)
err := client.PullImage("busybox", nil)
err := client.PullImage("busybox", nil, nil)
if err != nil {
t.Fatal("unable to pull busybox")
t.Fatal("unable to pull busybox: %v", err)
}

err = client.PullImage("haproxy", nil)
err = client.PullImage("haproxy", nil, nil)
if err != nil {
t.Fatal("unable to pull haproxy")
t.Fatal("unable to pull haproxy: %v", err)
}

err = client.PullImage("wrongimg", nil)
err = client.PullImage("wrongimg", nil, nil)
if err == nil {
t.Fatal("should return error when it fails to pull wrongimg")
}
Expand Down
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Client interface {
StopAllMonitorStats()
TagImage(nameOrID string, repo string, tag string, force bool) error
Version() (*Version, error)
PullImage(name string, auth *AuthConfig) error
PullImage(name string, auth *AuthConfig, cliOut io.Writer) error
LoadImage(reader io.Reader) error
RemoveContainer(id string, force, volumes bool) error
ListImages() ([]*Image, error)
Expand Down
4 changes: 2 additions & 2 deletions mockclient/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func (client *MockClient) Version() (*dockerclient.Version, error) {
return args.Get(0).(*dockerclient.Version), args.Error(1)
}

func (client *MockClient) PullImage(name string, auth *dockerclient.AuthConfig) error {
args := client.Mock.Called(name, auth)
func (client *MockClient) PullImage(name string, auth *dockerclient.AuthConfig, cliOut io.Writer) error {
args := client.Mock.Called(name, auth, cliOut)
return args.Error(0)
}

Expand Down