/
client.go
58 lines (50 loc) · 1.39 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
package dockerop
import (
"context"
"net/http"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
// OpClient the client operating docker engine
type OpClient struct {
client *client.Client
// others
Auth types.AuthConfig
Time time.Time // set in RegistryLogin
IdentityToken string // returned in RegistryLogin
Status string // returned in RegistryLogin
}
// NewOpClient return a pointer to OpClient
func NewOpClient(host string, httpcli *http.Client) (*OpClient, error) {
cli, err := client.NewClient(host, "v1.40", httpcli, nil)
if err != nil {
return nil, err
}
var opCli = &OpClient{
client: cli,
}
return opCli, nil
}
// Close close the connection to docker engine
func (c *OpClient) Close() error {
return c.client.Close()
}
// Ping pings the server
func (c *OpClient) Ping(ctx context.Context) (types.Ping, error) {
return c.client.Ping(ctx)
}
// Info returns the information about the docker engine
func (c *OpClient) Info(ctx context.Context) (types.Info, error) {
return c.client.Info(ctx)
}
// RegistryLogin authenticates the docker server with a given docker registry.
func (c *OpClient) RegistryLogin(ctx context.Context, auth types.AuthConfig) error {
body, err := c.client.RegistryLogin(ctx, auth)
if err == nil {
c.Time = time.Now()
c.IdentityToken = body.IdentityToken
c.Status = body.Status
}
return err
}