mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
client: make ServiceLogsResult an interface
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/moby/moby/client/internal/timestamp"
|
||||
@@ -26,16 +25,15 @@ type ServiceLogsOptions struct {
|
||||
// ServiceLogsResult holds the result of a service logs operation.
|
||||
// It implements [io.ReadCloser].
|
||||
// It's up to the caller to close the stream.
|
||||
type ServiceLogsResult struct {
|
||||
rc io.ReadCloser
|
||||
close func() error
|
||||
type ServiceLogsResult interface {
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
// ServiceLogs returns the logs generated by a service in an [ServiceLogsResult].
|
||||
func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options ServiceLogsOptions) (ServiceLogsResult, error) {
|
||||
serviceID, err := trimID("service", serviceID)
|
||||
if err != nil {
|
||||
return ServiceLogsResult{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
@@ -50,7 +48,7 @@ func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options Se
|
||||
if options.Since != "" {
|
||||
ts, err := timestamp.GetTimestamp(options.Since, time.Now())
|
||||
if err != nil {
|
||||
return ServiceLogsResult{}, fmt.Errorf(`invalid value for "since": %w`, err)
|
||||
return nil, fmt.Errorf(`invalid value for "since": %w`, err)
|
||||
}
|
||||
query.Set("since", ts)
|
||||
}
|
||||
@@ -70,33 +68,33 @@ func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options Se
|
||||
|
||||
resp, err := cli.get(ctx, "/services/"+serviceID+"/logs", query, nil)
|
||||
if err != nil {
|
||||
return ServiceLogsResult{}, err
|
||||
return nil, err
|
||||
}
|
||||
return newServiceLogsResult(resp.Body), nil
|
||||
return &serviceLogsResult{
|
||||
body: resp.Body,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newServiceLogsResult(rc io.ReadCloser) ServiceLogsResult {
|
||||
if rc == nil {
|
||||
panic("nil io.ReadCloser")
|
||||
}
|
||||
return ServiceLogsResult{
|
||||
rc: rc,
|
||||
close: sync.OnceValue(rc.Close),
|
||||
}
|
||||
type serviceLogsResult struct {
|
||||
// body must be closed to avoid a resource leak
|
||||
body io.ReadCloser
|
||||
}
|
||||
|
||||
// Read implements [io.ReadCloser] for LogsResult.
|
||||
func (r ServiceLogsResult) Read(p []byte) (n int, err error) {
|
||||
if r.rc == nil {
|
||||
var (
|
||||
_ io.ReadCloser = (*serviceLogsResult)(nil)
|
||||
_ ServiceLogsResult = (*serviceLogsResult)(nil)
|
||||
)
|
||||
|
||||
func (r *serviceLogsResult) 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 ServiceLogsResult) Close() error {
|
||||
if r.close == nil {
|
||||
func (r *serviceLogsResult) Close() error {
|
||||
if r == nil || r.body == nil {
|
||||
return nil
|
||||
}
|
||||
return r.close()
|
||||
return r.body.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user