client: make TaskLogsResult an interface

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-11-03 18:23:57 +01:00
parent 7dc9d39ca1
commit 5c8a9b7b9e
2 changed files with 42 additions and 46 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"io"
"net/url"
"sync"
"time"
"github.com/moby/moby/client/internal/timestamp"
@@ -24,9 +23,8 @@ type TaskLogsOptions struct {
// TaskLogsResult holds the result of a task logs operation.
// It implements [io.ReadCloser].
type TaskLogsResult struct {
rc io.ReadCloser
close func() error
type TaskLogsResult interface {
io.ReadCloser
}
// TaskLogs returns the logs generated by a task.
@@ -44,7 +42,7 @@ func (cli *Client) TaskLogs(ctx context.Context, taskID string, options TaskLogs
if options.Since != "" {
ts, err := timestamp.GetTimestamp(options.Since, time.Now())
if err != nil {
return TaskLogsResult{}, err
return nil, err
}
query.Set("since", ts)
}
@@ -64,33 +62,33 @@ func (cli *Client) TaskLogs(ctx context.Context, taskID string, options TaskLogs
resp, err := cli.get(ctx, "/tasks/"+taskID+"/logs", query, nil)
if err != nil {
return TaskLogsResult{}, err
return nil, err
}
return newTaskLogsResult(resp.Body), nil
return &taskLogsResult{
body: resp.Body,
}, nil
}
func newTaskLogsResult(rc io.ReadCloser) TaskLogsResult {
if rc == nil {
panic("nil io.ReadCloser")
}
return TaskLogsResult{
rc: rc,
close: sync.OnceValue(rc.Close),
}
type taskLogsResult struct {
// body must be closed to avoid a resource leak
body io.ReadCloser
}
// Read implements [io.ReadCloser] for LogsResult.
func (r TaskLogsResult) Read(p []byte) (n int, err error) {
if r.rc == nil {
var (
_ io.ReadCloser = (*taskLogsResult)(nil)
_ ContainerLogsResult = (*taskLogsResult)(nil)
)
func (r *taskLogsResult) Read(p []byte) (int, error) {
if r == nil || r.body == nil {
return 0, io.EOF
}
return r.rc.Read(p)
return r.body.Read(p)
}
// Close implements [io.ReadCloser] for LogsResult.
func (r TaskLogsResult) Close() error {
if r.close == nil {
func (r *taskLogsResult) Close() error {
if r == nil || r.body == nil {
return nil
}
return r.close()
return r.body.Close()
}