Skip to content

Commit 72da76f

Browse files
authored
Merge pull request #7 from taubyte/6-local-image-usage-fixes
better use case of local image
2 parents 8fd98eb + 36a5657 commit 72da76f

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

image.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717

1818
// Image initializes the given image, and attempts to pull the container from docker hub.
1919
// If the Build() Option is provided then the given DockerFile tarball is built and returned.
20-
func (c *Client) Image(ctx context.Context, image string, options ...ImageOption) (_image *Image, err error) {
21-
_image = &Image{
20+
func (c *Client) Image(ctx context.Context, image string, options ...ImageOption) (*Image, error) {
21+
_image := &Image{
2222
client: c,
2323
image: image,
2424
}
@@ -30,9 +30,10 @@ func (c *Client) Image(ctx context.Context, image string, options ...ImageOption
3030
}
3131
}
3232

33+
imageExists := _image.checkImageExists(ctx)
3334
if _image.buildTarball != nil {
34-
if ForceRebuild == true || _image.checkImage(ctx) != nil {
35-
err = _image.buildImage(ctx)
35+
if ForceRebuild == true || !imageExists {
36+
err := _image.buildImage(ctx)
3637
if err != nil {
3738
return nil, fmt.Errorf("building image `%s` failed with %s", image, err)
3839
}
@@ -41,28 +42,24 @@ func (c *Client) Image(ctx context.Context, image string, options ...ImageOption
4142
return _image, nil
4243
}
4344

44-
_image, err = _image.Pull(ctx)
45-
if err != nil {
45+
_image, err := _image.Pull(ctx)
46+
if err != nil && !imageExists {
4647
return nil, fmt.Errorf("pulling image `%s` failed with %s", image, err)
4748
}
4849

49-
return _image, err
50+
return _image, nil
5051
}
5152

5253
// checkImage checks the docker host client if the image is known.
53-
func (i *Image) checkImage(ctx context.Context) error {
54+
func (i *Image) checkImageExists(ctx context.Context) bool {
5455
res, err := i.client.ImageList(ctx, types.ImageListOptions{
5556
Filters: NewFilter("reference", i.image),
5657
})
57-
if err != nil {
58-
return fmt.Errorf("listing docker host images with reference `%s` failed with %s", i.image, err)
58+
if err != nil || len(res) < 1 {
59+
return false
5960
}
6061

61-
if len(res) < 1 {
62-
return fmt.Errorf("image `%s` not located in docker host", i.image)
63-
}
64-
65-
return nil
62+
return true
6663
}
6764

6865
// buildImage builds a DockerFile tarball as a docker image.
@@ -93,7 +90,7 @@ func (i *Image) buildImage(ctx context.Context) error {
9390
func (i *Image) Pull(ctx context.Context) (*Image, error) {
9491
reader, err := i.client.ImagePull(ctx, i.image, types.ImagePullOptions{})
9592
if err != nil {
96-
return nil, fmt.Errorf("pulling image `%s`, failed with %s", i.image, err)
93+
return i, fmt.Errorf("pulling image `%s`, failed with %s", i.image, err)
9794
}
9895

9996
defer reader.Close()
@@ -113,7 +110,7 @@ func (i *Image) Pull(ctx context.Context) (*Image, error) {
113110
}
114111
err = json.Unmarshal(line, &status)
115112
if err != nil {
116-
return nil, fmt.Errorf("unmarshaling status from docker pull on image `%s` failed with: %s", i.image, err)
113+
return i, fmt.Errorf("unmarshaling status from docker pull on image `%s` failed with: %s", i.image, err)
117114
}
118115
}
119116

0 commit comments

Comments
 (0)