client/inspect: Better Raw handling

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-10-20 12:57:54 +02:00
parent ee22a50b75
commit abf5679049
4 changed files with 40 additions and 24 deletions

View File

@@ -1,8 +1,12 @@
package client
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
cerrdefs "github.com/containerd/errdefs"
@@ -65,3 +69,18 @@ func encodePlatform(platform *ocispec.Platform) (string, error) {
}
return string(p), nil
}
func decodeWithRaw[T any](resp *http.Response, out *T) (raw []byte, _ error) {
if resp == nil || resp.Body == nil {
return nil, errors.New("empty response")
}
defer resp.Body.Close()
var buf bytes.Buffer
tr := io.TeeReader(resp.Body, &buf)
err := json.NewDecoder(tr).Decode(out)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}