Skip to content

Commit

Permalink
Alternative client opts !fixup! c8d/inspect: Add Manifests field
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
  • Loading branch information
vvoland committed Jan 29, 2025
1 parent 4219ce7 commit 51bfa52
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
46 changes: 11 additions & 35 deletions client/image_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,21 @@ import (
"github.com/docker/docker/api/types/image"
)

type imageInspectClientOpt struct {
raw *bytes.Buffer
apiOptions image.InspectOptions
}

type ImageInspectOpt func(*imageInspectClientOpt)

// ImageInspectWithRawResponse instructs the client to additionally store the
// raw inspect response in the provided buffer.
func ImageInspectWithRawResponse(raw *bytes.Buffer) ImageInspectOpt {
return func(opts *imageInspectClientOpt) {
opts.raw = raw
}
}
type ImageInspectOpts struct {
image.InspectOptions

// ImageInspectWithOpts sets the API options for the image inspect operation.
func ImageInspectWithOpts(opts image.InspectOptions) ImageInspectOpt {
return func(clientOpts *imageInspectClientOpt) {
clientOpts.apiOptions = opts
}
// Raw is an optional writer to write the raw inspect response to.
Raw io.Writer
}

// ImageInspect returns the image information.
func (cli *Client) ImageInspect(ctx context.Context, imageID string, inspectOpts ...ImageInspectOpt) (image.InspectResponse, error) {
func (cli *Client) ImageInspect(ctx context.Context, imageID string, opts ImageInspectOpts) (image.InspectResponse, error) {
if imageID == "" {
return image.InspectResponse{}, objectNotFoundError{object: "image", id: imageID}
}

var opts imageInspectClientOpt
for _, opt := range inspectOpts {
opt(&opts)
}

query := url.Values{}
if opts.apiOptions.Manifests {
if opts.Manifests {
if err := cli.NewVersionError(ctx, "1.48", "manifests"); err != nil {
return image.InspectResponse{}, err
}
Expand All @@ -57,17 +37,13 @@ func (cli *Client) ImageInspect(ctx context.Context, imageID string, inspectOpts
return image.InspectResponse{}, err
}

buf := opts.raw
if buf == nil {
buf = &bytes.Buffer{}
}

if _, err := io.Copy(buf, serverResp.body); err != nil {
return image.InspectResponse{}, err
var reader io.Reader = serverResp.body
if opts.Raw != nil {
reader = io.TeeReader(serverResp.body, opts.Raw)
}

var response image.InspectResponse
err = json.Unmarshal(buf.Bytes(), &response)
err = json.NewDecoder(reader).Decode(&response)
return response, err
}

Expand All @@ -76,7 +52,7 @@ func (cli *Client) ImageInspect(ctx context.Context, imageID string, inspectOpts
// TODeprecate: Use [ImageInspect] instead.
func (cli *Client) ImageInspectWithRaw(ctx context.Context, imageID string) (image.InspectResponse, []byte, error) {
var buf bytes.Buffer
resp, err := cli.ImageInspect(ctx, imageID, ImageInspectWithRawResponse(&buf))
resp, err := cli.ImageInspect(ctx, imageID, ImageInspectOpts{Raw: &buf})
if err != nil {
return image.InspectResponse{}, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type ImageAPIClient interface {
ImageHistory(ctx context.Context, image string, opts image.HistoryOptions) ([]image.HistoryResponseItem, error)
ImageImport(ctx context.Context, source image.ImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error)
ImageInspectWithRaw(ctx context.Context, image string) (image.InspectResponse, []byte, error)
ImageInspect(ctx context.Context, image string, _ ...ImageInspectOpt) (image.InspectResponse, error)
ImageInspect(ctx context.Context, image string, opts ImageInspectOpts) (image.InspectResponse, error)
ImageList(ctx context.Context, options image.ListOptions) ([]image.Summary, error)
ImageLoad(ctx context.Context, input io.Reader, opts image.LoadOptions) (image.LoadResponse, error)
ImagePull(ctx context.Context, ref string, options image.PullOptions) (io.ReadCloser, error)
Expand Down

0 comments on commit 51bfa52

Please sign in to comment.