mirror of
https://github.com/moby/moby.git
synced 2026-01-11 02:31:44 +00:00
client: make ImageImportResult an interface
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
37
vendor/github.com/moby/moby/client/image_import.go
generated
vendored
37
vendor/github.com/moby/moby/client/image_import.go
generated
vendored
@@ -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()
|
||||
}
|
||||
|
||||
19
vendor/github.com/moby/moby/client/image_import_opts.go
generated
vendored
19
vendor/github.com/moby/moby/client/image_import_opts.go
generated
vendored
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user