This repository has been archived by the owner on Dec 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
/
client.go
68 lines (57 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
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
apiVersion string
}
// NewClientConfig create docker client config
func NewClientConfig(endpoint, cert, key, ca, apiVersion string) *ClientConfig {
return &ClientConfig{
endpoint: endpoint,
certPath: cert,
keyPath: key,
caPath: ca,
apiVersion: apiVersion,
}
}
// NewDocker create new docker client
func NewDocker(config *ClientConfig) *Docker {
if os.Getenv("DOCKER_HOST") != "" {
client, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(config.apiVersion))
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.apiVersion))
if err != nil {
panic(err)
}
return &Docker{client}
}
// default api version 1.39
client, err := client.NewClientWithOpts(client.WithHost(config.endpoint), client.WithVersion(config.apiVersion))
if err != nil {
panic(err)
}
Client = &Docker{client}
return Client
}