Skip to content

Commit 639a121

Browse files
committed
client/image-inspect: Introduce client opts
Deprecate ImageInspectWithRaw and add a simpler ImageInspect function which takes optional options. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent 6664f12 commit 639a121

3 files changed

Lines changed: 77 additions & 15 deletions

File tree

client/client_interfaces.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ type ImageAPIClient interface {
115115
ImageCreate(ctx context.Context, parentReference string, options image.CreateOptions) (io.ReadCloser, error)
116116
ImageHistory(ctx context.Context, image string, opts image.HistoryOptions) ([]image.HistoryResponseItem, error)
117117
ImageImport(ctx context.Context, source image.ImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error)
118+
// Deprecated: Use [Client.ImageInspect] instead.
119+
// Raw response can be obtained by [ImageInspectWithRawResponse] option.
118120
ImageInspectWithRaw(ctx context.Context, image string) (image.InspectResponse, []byte, error)
121+
ImageInspect(ctx context.Context, image string, _ ...ImageInspectOption) (image.InspectResponse, error)
119122
ImageList(ctx context.Context, options image.ListOptions) ([]image.Summary, error)
120123
ImageLoad(ctx context.Context, input io.Reader, opts image.LoadOptions) (image.LoadResponse, error)
121124
ImagePull(ctx context.Context, ref string, options image.PullOptions) (io.ReadCloser, error)

client/image_inspect.go

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,88 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"io"
9+
"net/url"
810

911
"github.com/docker/docker/api/types/image"
1012
)
1113

12-
// ImageInspectWithRaw returns the image information and its raw representation.
13-
func (cli *Client) ImageInspectWithRaw(ctx context.Context, imageID string) (image.InspectResponse, []byte, error) {
14+
// ImageInspectOption is a type representing functional options for the image inspect operation.
15+
type ImageInspectOption interface {
16+
Apply(*imageInspectOpts) error
17+
}
18+
type imageInspectOptionFunc func(opt *imageInspectOpts) error
19+
20+
func (f imageInspectOptionFunc) Apply(o *imageInspectOpts) error {
21+
return f(o)
22+
}
23+
24+
// ImageInspectWithRawResponse instructs the client to additionally store the
25+
// raw inspect response in the provided buffer.
26+
func ImageInspectWithRawResponse(raw *bytes.Buffer) ImageInspectOption {
27+
return imageInspectOptionFunc(func(opts *imageInspectOpts) error {
28+
opts.raw = raw
29+
return nil
30+
})
31+
}
32+
33+
// ImageInspectWithAPIOpts sets the API options for the image inspect operation.
34+
func ImageInspectWithAPIOpts(opts image.InspectOptions) ImageInspectOption {
35+
return imageInspectOptionFunc(func(clientOpts *imageInspectOpts) error {
36+
clientOpts.apiOptions = opts
37+
return nil
38+
})
39+
}
40+
41+
type imageInspectOpts struct {
42+
raw *bytes.Buffer
43+
apiOptions image.InspectOptions
44+
}
45+
46+
// ImageInspect returns the image information.
47+
func (cli *Client) ImageInspect(ctx context.Context, imageID string, inspectOpts ...ImageInspectOption) (image.InspectResponse, error) {
1448
if imageID == "" {
15-
return image.InspectResponse{}, nil, objectNotFoundError{object: "image", id: imageID}
49+
return image.InspectResponse{}, objectNotFoundError{object: "image", id: imageID}
50+
}
51+
52+
var opts imageInspectOpts
53+
for _, opt := range inspectOpts {
54+
if err := opt.Apply(&opts); err != nil {
55+
return image.InspectResponse{}, fmt.Errorf("error applying image inspect option: %w", err)
56+
}
1657
}
17-
serverResp, err := cli.get(ctx, "/images/"+imageID+"/json", nil, nil)
58+
59+
query := url.Values{}
60+
serverResp, err := cli.get(ctx, "/images/"+imageID+"/json", query, nil)
1861
defer ensureReaderClosed(serverResp)
1962
if err != nil {
20-
return image.InspectResponse{}, nil, err
63+
return image.InspectResponse{}, err
2164
}
2265

23-
body, err := io.ReadAll(serverResp.body)
24-
if err != nil {
25-
return image.InspectResponse{}, nil, err
66+
buf := opts.raw
67+
if buf == nil {
68+
buf = &bytes.Buffer{}
69+
}
70+
71+
if _, err := io.Copy(buf, serverResp.body); err != nil {
72+
return image.InspectResponse{}, err
2673
}
2774

2875
var response image.InspectResponse
29-
rdr := bytes.NewReader(body)
30-
err = json.NewDecoder(rdr).Decode(&response)
31-
return response, body, err
76+
err = json.Unmarshal(buf.Bytes(), &response)
77+
return response, err
78+
}
79+
80+
// ImageInspectWithRaw returns the image information and its raw representation.
81+
//
82+
// Deprecated: Use [Client.ImageInspect] instead.
83+
// Raw response can be obtained by [ImageInspectWithRawResponse] option.
84+
func (cli *Client) ImageInspectWithRaw(ctx context.Context, imageID string) (image.InspectResponse, []byte, error) {
85+
var buf bytes.Buffer
86+
resp, err := cli.ImageInspect(ctx, imageID, ImageInspectWithRawResponse(&buf))
87+
if err != nil {
88+
return image.InspectResponse{}, nil, err
89+
}
90+
return resp, buf.Bytes(), err
3291
}

client/image_inspect_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestImageInspectError(t *testing.T) {
2323
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
2424
}
2525

26-
_, _, err := client.ImageInspectWithRaw(context.Background(), "nothing")
26+
_, err := client.ImageInspect(context.Background(), "nothing")
2727
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
2828
}
2929

@@ -32,7 +32,7 @@ func TestImageInspectImageNotFound(t *testing.T) {
3232
client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
3333
}
3434

35-
_, _, err := client.ImageInspectWithRaw(context.Background(), "unknown")
35+
_, err := client.ImageInspect(context.Background(), "unknown")
3636
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
3737
}
3838

@@ -42,7 +42,7 @@ func TestImageInspectWithEmptyID(t *testing.T) {
4242
return nil, errors.New("should not make request")
4343
}),
4444
}
45-
_, _, err := client.ImageInspectWithRaw(context.Background(), "")
45+
_, err := client.ImageInspect(context.Background(), "")
4646
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
4747
}
4848

@@ -68,7 +68,7 @@ func TestImageInspect(t *testing.T) {
6868
}),
6969
}
7070

71-
imageInspect, _, err := client.ImageInspectWithRaw(context.Background(), "image_id")
71+
imageInspect, err := client.ImageInspect(context.Background(), "image_id")
7272
if err != nil {
7373
t.Fatal(err)
7474
}

0 commit comments

Comments
 (0)