mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
client: refactor service api client functions for defined options/result structs
Co-authored-by: Claude <noreply@anthropic.com> Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
This commit is contained in:
@@ -4,14 +4,34 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/moby/moby/client/internal/timestamp"
|
||||
)
|
||||
|
||||
// TaskLogs returns the logs generated by a task in an [io.ReadCloser].
|
||||
// TaskLogsOptions holds parameters to filter logs with.
|
||||
type TaskLogsOptions struct {
|
||||
ShowStdout bool
|
||||
ShowStderr bool
|
||||
Since string
|
||||
Until string
|
||||
Timestamps bool
|
||||
Follow bool
|
||||
Tail string
|
||||
Details bool
|
||||
}
|
||||
|
||||
// TaskLogsResult holds the result of a task logs operation.
|
||||
// It implements [io.ReadCloser].
|
||||
type TaskLogsResult struct {
|
||||
rc io.ReadCloser
|
||||
close func() error
|
||||
}
|
||||
|
||||
// TaskLogs returns the logs generated by a task.
|
||||
// It's up to the caller to close the stream.
|
||||
func (cli *Client) TaskLogs(ctx context.Context, taskID string, options ContainerLogsOptions) (io.ReadCloser, error) {
|
||||
func (cli *Client) TaskLogs(ctx context.Context, taskID string, options TaskLogsOptions) (TaskLogsResult, error) {
|
||||
query := url.Values{}
|
||||
if options.ShowStdout {
|
||||
query.Set("stdout", "1")
|
||||
@@ -24,7 +44,7 @@ func (cli *Client) TaskLogs(ctx context.Context, taskID string, options Containe
|
||||
if options.Since != "" {
|
||||
ts, err := timestamp.GetTimestamp(options.Since, time.Now())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return TaskLogsResult{}, err
|
||||
}
|
||||
query.Set("since", ts)
|
||||
}
|
||||
@@ -44,7 +64,33 @@ func (cli *Client) TaskLogs(ctx context.Context, taskID string, options Containe
|
||||
|
||||
resp, err := cli.get(ctx, "/tasks/"+taskID+"/logs", query, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return TaskLogsResult{}, err
|
||||
}
|
||||
return resp.Body, nil
|
||||
return newTaskLogsResult(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),
|
||||
}
|
||||
}
|
||||
|
||||
// Read implements [io.ReadCloser] for LogsResult.
|
||||
func (r TaskLogsResult) Read(p []byte) (n int, err error) {
|
||||
if r.rc == nil {
|
||||
return 0, io.EOF
|
||||
}
|
||||
return r.rc.Read(p)
|
||||
}
|
||||
|
||||
// Close implements [io.ReadCloser] for LogsResult.
|
||||
func (r TaskLogsResult) Close() error {
|
||||
if r.close == nil {
|
||||
return nil
|
||||
}
|
||||
return r.close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user