Skip to content

Commit abf5679

Browse files
committed
client/inspect: Better Raw handling
Signed-off-by: Paweł Gronowski <[email protected]>
1 parent ee22a50 commit abf5679

4 files changed

Lines changed: 40 additions & 24 deletions

File tree

client/config_inspect.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package client
22

33
import (
4-
"bytes"
54
"context"
6-
"encoding/json"
7-
"io"
85

96
"github.com/moby/moby/api/types/swarm"
107
)
@@ -32,15 +29,7 @@ func (cli *Client) ConfigInspect(ctx context.Context, id string, options ConfigI
3229
return ConfigInspectResult{}, err
3330
}
3431

35-
body, err := io.ReadAll(resp.Body)
36-
if err != nil {
37-
return ConfigInspectResult{}, err
38-
}
39-
4032
var out ConfigInspectResult
41-
out.Raw = body
42-
rdr := bytes.NewReader(body)
43-
err = json.NewDecoder(rdr).Decode(&out.Config)
44-
33+
out.Raw, err = decodeWithRaw(resp, &out.Config)
4534
return out, err
4635
}

client/utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package client
22

33
import (
4+
"bytes"
45
"encoding/json"
6+
"errors"
57
"fmt"
8+
"io"
9+
"net/http"
610
"strings"
711

812
cerrdefs "github.com/containerd/errdefs"
@@ -65,3 +69,18 @@ func encodePlatform(platform *ocispec.Platform) (string, error) {
6569
}
6670
return string(p), nil
6771
}
72+
73+
func decodeWithRaw[T any](resp *http.Response, out *T) (raw []byte, _ error) {
74+
if resp == nil || resp.Body == nil {
75+
return nil, errors.New("empty response")
76+
}
77+
defer resp.Body.Close()
78+
79+
var buf bytes.Buffer
80+
tr := io.TeeReader(resp.Body, &buf)
81+
err := json.NewDecoder(tr).Decode(out)
82+
if err != nil {
83+
return nil, err
84+
}
85+
return buf.Bytes(), nil
86+
}

vendor/github.com/moby/moby/client/config_inspect.go

Lines changed: 1 addition & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/moby/client/utils.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)