-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.go
55 lines (42 loc) · 1.21 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
package main
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/client"
)
type DockerClientWrapper struct {
client *client.Client
}
func (dc DockerClientWrapper) GetClient() *client.Client {
if dc.client == nil {
client, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
dc.client = client
}
return dc.client
}
func (dc DockerClientWrapper) GetDeamonEventStreams() (<-chan events.Message, <-chan error) {
return dc.GetClient().Events(context.Background(), types.EventsOptions{})
}
func (dc DockerClientWrapper) GetContainerList(all bool) []types.Container {
containers, err := dc.GetClient().ContainerList(context.Background(), types.ContainerListOptions{All: all})
if err != nil {
panic(err)
}
return containers
}
func (dc DockerClientWrapper) StartContainer(containerID string) {
err := dc.GetClient().ContainerStart(context.Background(), containerID, types.ContainerStartOptions{})
if err != nil {
panic(err)
}
}
func (dc DockerClientWrapper) StopContainer(containerID string) {
err := dc.GetClient().ContainerStop(context.Background(), containerID, nil)
if err != nil {
panic(err)
}
}