mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
client: make TaskLogsResult an interface
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user