This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
/
client.go
70 lines (56 loc) 路 1.42 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
64
65
66
67
68
69
70
package docker
import (
"os"
"github.com/docker/docker/client"
)
// Client docker client
var Client *Docker
// Docker docker client
type Docker struct {
*client.Client
}
// ClientConfig docker client config
type ClientConfig struct {
endpoint string
certPath string
keyPath string
caPath string
engineVersion string
}
// NewClientConfig create docker client config
func NewClientConfig(endpoint, cert, key, ca, engineVersion string) *ClientConfig {
return &ClientConfig{
endpoint: endpoint,
certPath: cert,
keyPath: key,
caPath: ca,
engineVersion: engineVersion,
}
}
// NewDocker create new docker client
func NewDocker(config *ClientConfig) *Docker {
if os.Getenv("DOCKER_HOST") != "" {
client, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(config.engineVersion))
if err != nil {
panic(err)
}
return &Docker{client}
}
if config.caPath != "" &&
config.certPath != "" &&
config.keyPath != "" {
client, err := client.NewClientWithOpts(client.WithTLSClientConfig(config.caPath, config.certPath, config.keyPath),
client.WithHost(config.endpoint),
client.WithVersion(config.engineVersion))
if err != nil {
panic(err)
}
return &Docker{client}
}
client, err := client.NewClientWithOpts(client.WithHost(config.endpoint), client.WithVersion(config.engineVersion))
if err != nil {
panic(err)
}
Client = &Docker{client}
return Client
}