Skip to content

Commit b21ab19

Browse files
authored
simple client has Docker field rather than inheriting all methods of the docker client (#15)
1 parent 0a51a06 commit b21ab19

File tree

6 files changed

+16
-16
lines changed

6 files changed

+16
-16
lines changed

container.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import (
1010

1111
// Run starts the container and waits for the container to exit before returning the container logs.
1212
func (c *Container) Run(ctx context.Context) (*MuxedReadCloser, error) {
13-
if err := c.image.client.ContainerStart(ctx, c.id, types.ContainerStartOptions{}); err != nil {
13+
if err := c.image.client.Docker.ContainerStart(ctx, c.id, types.ContainerStartOptions{}); err != nil {
1414
return nil, errorContainerStart(c.id, c.image.image, err)
1515
}
1616

1717
if err := c.Wait(ctx); err != nil {
1818
return nil, err
1919
}
2020

21-
info, err := c.image.client.ContainerInspect(ctx, c.id)
21+
info, err := c.image.client.Docker.ContainerInspect(ctx, c.id)
2222
if err != nil {
2323
return nil, errorContainerInspect(c.id, c.image.image, err)
2424
}
@@ -28,7 +28,7 @@ func (c *Container) Run(ctx context.Context) (*MuxedReadCloser, error) {
2828
RetCodeErr = errorContainerExitCode(c.id, c.image.image, info.ContainerJSONBase.State.ExitCode)
2929
}
3030

31-
muxed, err := c.image.client.ContainerLogs(ctx, c.id, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
31+
muxed, err := c.image.client.Docker.ContainerLogs(ctx, c.id, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true})
3232
if err != nil {
3333
return nil, errorContainerLogs(c.id, c.image.image, err)
3434
}
@@ -41,7 +41,7 @@ func (c *Container) Run(ctx context.Context) (*MuxedReadCloser, error) {
4141
// Wait calls the ContainerWait method for the container, and returns once a response has been received.
4242
// If there is an error response then wait will return the error
4343
func (c *Container) Wait(ctx context.Context) error {
44-
statusCh, errCh := c.image.client.ContainerWait(ctx, c.id, container.WaitConditionNotRunning)
44+
statusCh, errCh := c.image.client.Docker.ContainerWait(ctx, c.id, container.WaitConditionNotRunning)
4545
select {
4646
case err := <-errCh:
4747
if err != nil {
@@ -54,7 +54,7 @@ func (c *Container) Wait(ctx context.Context) error {
5454

5555
// Cleanup removes the container from the docker host client.
5656
func (c *Container) Cleanup(ctx context.Context) error {
57-
if err := c.image.client.ContainerRemove(ctx, c.id, types.ContainerRemoveOptions{}); err != nil {
57+
if err := c.image.client.Docker.ContainerRemove(ctx, c.id, types.ContainerRemoveOptions{}); err != nil {
5858
return fmt.Errorf("removing container with id `%s` failed with: %s", c.id, err)
5959
}
6060
return nil

gc/new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Start(ctx context.Context, options ...Option) error {
3737
}
3838

3939
go func() {
40-
defer client.Close()
40+
defer client.Docker.Close()
4141
for {
4242
select {
4343
case <-time.After(cnf.interval):

image.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (c *Client) Image(ctx context.Context, name string, options ...ImageOption)
4545

4646
// checkImage checks the docker host client if the image is known.
4747
func (i *Image) checkImageExists(ctx context.Context) bool {
48-
res, err := i.client.ImageList(ctx, types.ImageListOptions{
48+
res, err := i.client.Docker.ImageList(ctx, types.ImageListOptions{
4949
Filters: NewFilter("reference", i.image),
5050
})
5151

@@ -54,7 +54,7 @@ func (i *Image) checkImageExists(ctx context.Context) bool {
5454

5555
// buildImage builds a DockerFile tarball as a docker image.
5656
func (i *Image) buildImage(ctx context.Context) error {
57-
imageBuildResponse, err := i.client.ImageBuild(
57+
imageBuildResponse, err := i.client.Docker.ImageBuild(
5858
ctx,
5959
i.buildTarball,
6060
types.ImageBuildOptions{
@@ -78,7 +78,7 @@ func (i *Image) buildImage(ctx context.Context) error {
7878

7979
// Pull retrieves latest changes to the image from docker hub.
8080
func (i *Image) Pull(ctx context.Context) (*Image, error) {
81-
reader, err := i.client.ImagePull(ctx, i.image, types.ImagePullOptions{})
81+
reader, err := i.client.Docker.ImagePull(ctx, i.image, types.ImagePullOptions{})
8282
if err != nil {
8383
return i, errorClientPull(err)
8484
}
@@ -137,7 +137,7 @@ func (i *Image) Instantiate(ctx context.Context, options ...ContainerOption) (*C
137137
config.WorkingDir = c.workDir
138138
}
139139

140-
resp, err := c.image.client.ContainerCreate(ctx, config, &container.HostConfig{Mounts: mounts}, nil, nil, "")
140+
resp, err := c.image.client.Docker.ContainerCreate(ctx, config, &container.HostConfig{Mounts: mounts}, nil, nil, "")
141141
if err != nil {
142142
return nil, errorContainerCreate(c.image.Name(), err)
143143
}
@@ -148,13 +148,13 @@ func (i *Image) Instantiate(ctx context.Context, options ...ContainerOption) (*C
148148

149149
// Clean removes all docker images that match the given filter, and max age.
150150
func (c *Client) Clean(ctx context.Context, age time.Duration, filter filters.Args) error {
151-
images, _ := c.ImageList(ctx, types.ImageListOptions{Filters: filter})
151+
images, _ := c.Docker.ImageList(ctx, types.ImageListOptions{Filters: filter})
152152
timeNow := time.Now()
153153

154154
var err error
155155
for _, image := range images {
156156
if time.Unix(image.Created, 0).Add(age).Before(timeNow) {
157-
if _, _err := c.ImageRemove(ctx, image.ID, types.ImageRemoveOptions{
157+
if _, _err := c.Docker.ImageRemove(ctx, image.ID, types.ImageRemoveOptions{
158158
Force: true,
159159
PruneChildren: true,
160160
}); _err != nil {

new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
// New creates a new dockerClient with default Options.
1010
func New() (dockerClient *Client, err error) {
1111
dockerClient = new(Client)
12-
dockerClient.Client, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
12+
dockerClient.Docker, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
1313
if err != nil {
1414
return nil, fmt.Errorf("new docker client failed with: %w", err)
1515
}

tests/all_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestContainerCleanUpInterval(t *testing.T) {
125125
return
126126
}
127127

128-
images, err := cli.ImageList(ctx, types.ImageListOptions{
128+
images, err := cli.Docker.ImageList(ctx, types.ImageListOptions{
129129
Filters: ci.NewFilter("reference", testCustomImage),
130130
})
131131
if err != nil {
@@ -140,7 +140,7 @@ func TestContainerCleanUpInterval(t *testing.T) {
140140

141141
time.Sleep(20 * time.Second)
142142

143-
images, err = cli.ImageList(ctx, types.ImageListOptions{
143+
images, err = cli.Docker.ImageList(ctx, types.ImageListOptions{
144144
Filters: ci.NewFilter("reference", testCustomImage),
145145
})
146146
if err != nil {

types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type MuxedReadCloser struct {
1717

1818
// Client wraps the methods of the docker Client.
1919
type Client struct {
20-
*client.Client
20+
Docker *client.Client
2121
}
2222

2323
// volume defines the source and target to be volumed in the docker container.

0 commit comments

Comments
 (0)