client: make ImageImportResult an interface

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-11-03 17:46:11 +01:00
parent 5fc866fbfd
commit 5bfc628e97
4 changed files with 68 additions and 44 deletions

View File

@@ -2,18 +2,24 @@ package client
import (
"context"
"io"
"net/url"
"github.com/distribution/reference"
)
// ImageImportResult holds the response body returned by the daemon for image import.
type ImageImportResult interface {
io.ReadCloser
}
// ImageImport creates a new image based on the source options.
// It returns the JSON content in the response body.
func (cli *Client) ImageImport(ctx context.Context, source ImageImportSource, ref string, options ImageImportOptions) (ImageImportResult, error) {
if ref != "" {
// Check if the given image name can be resolved
if _, err := reference.ParseNormalizedNamed(ref); err != nil {
return ImageImportResult{}, err
return nil, err
}
}
@@ -40,7 +46,32 @@ func (cli *Client) ImageImport(ctx context.Context, source ImageImportSource, re
resp, err := cli.postRaw(ctx, "/images/create", query, source.Source, nil)
if err != nil {
return ImageImportResult{}, err
return nil, err
}
return ImageImportResult{rc: resp.Body}, nil
return &imageImportResult{body: resp.Body}, nil
}
// ImageImportResult holds the response body returned by the daemon for image import.
type imageImportResult struct {
// body must be closed to avoid a resource leak
body io.ReadCloser
}
var (
_ io.ReadCloser = (*imageImportResult)(nil)
_ ImageImportResult = (*imageImportResult)(nil)
)
func (r *imageImportResult) Read(p []byte) (int, error) {
if r == nil || r.body == nil {
return 0, io.EOF
}
return r.body.Read(p)
}
func (r *imageImportResult) Close() error {
if r == nil || r.body == nil {
return nil
}
return r.body.Close()
}

View File

@@ -19,22 +19,3 @@ type ImageImportOptions struct {
Changes []string // Changes are the raw changes to apply to this image
Platform ocispec.Platform // Platform is the target platform of the image
}
// ImageImportResult holds the response body returned by the daemon for image import.
type ImageImportResult struct {
rc io.ReadCloser
}
func (r ImageImportResult) Read(p []byte) (n int, err error) {
if r.rc == nil {
return 0, io.EOF
}
return r.rc.Read(p)
}
func (r ImageImportResult) Close() error {
if r.rc == nil {
return nil
}
return r.rc.Close()
}

View File

@@ -2,18 +2,24 @@ package client
import (
"context"
"io"
"net/url"
"github.com/distribution/reference"
)
// ImageImportResult holds the response body returned by the daemon for image import.
type ImageImportResult interface {
io.ReadCloser
}
// ImageImport creates a new image based on the source options.
// It returns the JSON content in the response body.
func (cli *Client) ImageImport(ctx context.Context, source ImageImportSource, ref string, options ImageImportOptions) (ImageImportResult, error) {
if ref != "" {
// Check if the given image name can be resolved
if _, err := reference.ParseNormalizedNamed(ref); err != nil {
return ImageImportResult{}, err
return nil, err
}
}
@@ -40,7 +46,32 @@ func (cli *Client) ImageImport(ctx context.Context, source ImageImportSource, re
resp, err := cli.postRaw(ctx, "/images/create", query, source.Source, nil)
if err != nil {
return ImageImportResult{}, err
return nil, err
}
return ImageImportResult{rc: resp.Body}, nil
return &imageImportResult{body: resp.Body}, nil
}
// ImageImportResult holds the response body returned by the daemon for image import.
type imageImportResult struct {
// body must be closed to avoid a resource leak
body io.ReadCloser
}
var (
_ io.ReadCloser = (*imageImportResult)(nil)
_ ImageImportResult = (*imageImportResult)(nil)
)
func (r *imageImportResult) Read(p []byte) (int, error) {
if r == nil || r.body == nil {
return 0, io.EOF
}
return r.body.Read(p)
}
func (r *imageImportResult) Close() error {
if r == nil || r.body == nil {
return nil
}
return r.body.Close()
}

View File

@@ -19,22 +19,3 @@ type ImageImportOptions struct {
Changes []string // Changes are the raw changes to apply to this image
Platform ocispec.Platform // Platform is the target platform of the image
}
// ImageImportResult holds the response body returned by the daemon for image import.
type ImageImportResult struct {
rc io.ReadCloser
}
func (r ImageImportResult) Read(p []byte) (n int, err error) {
if r.rc == nil {
return 0, io.EOF
}
return r.rc.Read(p)
}
func (r ImageImportResult) Close() error {
if r.rc == nil {
return nil
}
return r.rc.Close()
}