-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.go
78 lines (64 loc) · 1.45 KB
/
docker.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package docker
import (
"github.com/docker/docker/client"
"github.com/go-zoox/docker/container"
"github.com/go-zoox/docker/image"
"github.com/go-zoox/docker/info"
"github.com/go-zoox/docker/network"
"github.com/go-zoox/docker/volume"
)
// Docker is the client of docker
type Docker interface {
Info() info.Info
Container() container.Container
Image() image.Image
Network() network.Network
Volume() volume.Volume
}
type docker struct {
client *client.Client
}
// Config ...
type Config struct {
// Docker Host
Server string `json:"server"`
}
// Option ...
type Option func(cfg *Config)
// New creates a docker client.
func New(opts ...Option) (d Docker, err error) {
cfg := &Config{}
for _, o := range opts {
o(cfg)
}
var c *client.Client
if cfg.Server == "" {
c, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
} else {
c, err = client.NewClientWithOpts(client.WithHost(cfg.Server), client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
}
return &docker{
client: c,
}, nil
}
func (d *docker) Info() info.Info {
return info.New(d.client)
}
func (d *docker) Container() container.Container {
return container.New(d.client)
}
func (d *docker) Image() image.Image {
return image.New(d.client)
}
func (d *docker) Network() network.Network {
return network.New(d.client)
}
func (d *docker) Volume() volume.Volume {
return volume.New(d.client)
}