forked from iximiuz/cdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
63 lines (52 loc) · 1.16 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package docker
import (
"context"
"fmt"
"io"
"github.com/docker/cli/cli/streams"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
)
type Client struct {
client.CommonAPIClient
out *streams.Out
}
var _ client.CommonAPIClient = &Client{}
type Options struct {
Out *streams.Out
Host string
}
func NewClient(opts Options) (*Client, error) {
dockerOpts := []client.Opt{
client.FromEnv,
client.WithAPIVersionNegotiation(),
}
if len(opts.Host) > 0 {
dockerOpts = append(dockerOpts, client.WithHost(opts.Host))
}
inner, err := client.NewClientWithOpts(dockerOpts...)
if err != nil {
return nil, fmt.Errorf("cannot initialize Docker client: %w", err)
}
out := opts.Out
if out == nil {
out = streams.NewOut(io.Discard)
}
return &Client{
CommonAPIClient: inner,
out: out,
}, nil
}
func (c *Client) ImagePullEx(
ctx context.Context,
image string,
options types.ImagePullOptions,
) error {
resp, err := c.CommonAPIClient.ImagePull(ctx, image, options)
if err != nil {
return err
}
defer resp.Close()
return jsonmessage.DisplayJSONMessagesToStream(resp, c.out, nil)
}