mirror of
https://github.com/moby/moby.git
synced 2026-01-11 02:31:44 +00:00
Use a cancelReadCloser to automatically close the reader when the context is cancelled. Consumers are still recommended to manually close the reader, but the cancelReadCloser makes the Close idempotent. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/url"
|
|
)
|
|
|
|
// ContainerExportOptions specifies options for container export operations.
|
|
type ContainerExportOptions struct {
|
|
// Currently no options are defined for ContainerExport
|
|
}
|
|
|
|
// ContainerExportResult represents the result of a container export operation.
|
|
type ContainerExportResult interface {
|
|
io.ReadCloser
|
|
}
|
|
|
|
// ContainerExport retrieves the raw contents of a container
|
|
// and returns them as an [io.ReadCloser]. It's up to the caller
|
|
// to close the stream.
|
|
//
|
|
// The underlying [io.ReadCloser] is automatically closed if the context is canceled,
|
|
func (cli *Client) ContainerExport(ctx context.Context, containerID string, options ContainerExportOptions) (ContainerExportResult, error) {
|
|
containerID, err := trimID("container", containerID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := cli.get(ctx, "/containers/"+containerID+"/export", url.Values{}, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &containerExportResult{
|
|
ReadCloser: newCancelReadCloser(ctx, resp.Body),
|
|
}, nil
|
|
}
|
|
|
|
type containerExportResult struct {
|
|
io.ReadCloser
|
|
}
|
|
|
|
var (
|
|
_ io.ReadCloser = (*containerExportResult)(nil)
|
|
_ ContainerExportResult = (*containerExportResult)(nil)
|
|
)
|