@@ -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}
0 commit comments