refactor: use pkg directory structure
This commit is contained in:
52
pkg/client/client.go
Normal file
52
pkg/client/client.go
Normal file
@ -0,0 +1,52 @@
|
||||
// Package client provides Docker client initiatialisation functions.
|
||||
package client
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// New initiates a new Docker client.
|
||||
func New(contextName string) (*client.Client, error) {
|
||||
context, err := GetContext(contextName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctxEndpoint, err := GetContextEndpoint(context)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
helper := newConnectionHelper(ctxEndpoint)
|
||||
httpClient := &http.Client{
|
||||
// No tls, no proxy
|
||||
Transport: &http.Transport{
|
||||
DialContext: helper.Dialer,
|
||||
},
|
||||
}
|
||||
|
||||
var clientOpts []client.Opt
|
||||
clientOpts = append(clientOpts,
|
||||
client.WithHTTPClient(httpClient),
|
||||
client.WithHost(helper.Host),
|
||||
client.WithDialContext(helper.Dialer),
|
||||
)
|
||||
|
||||
version := os.Getenv("DOCKER_API_VERSION")
|
||||
if version != "" {
|
||||
clientOpts = append(clientOpts, client.WithVersion(version))
|
||||
} else {
|
||||
clientOpts = append(clientOpts, client.WithAPIVersionNegotiation())
|
||||
}
|
||||
|
||||
cl, err := client.NewClientWithOpts(clientOpts...)
|
||||
if err != nil {
|
||||
logrus.Fatalf("unable to create Docker client: %s", err)
|
||||
}
|
||||
|
||||
return cl, nil
|
||||
}
|
Reference in New Issue
Block a user