github.com/xiaobinqt/libcompose@v1.1.0/docker/client/client.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  
    10  	cliconfig "github.com/docker/cli/cli/config"
    11  	"github.com/docker/docker/client"
    12  	"github.com/docker/docker/pkg/homedir"
    13  	"github.com/docker/go-connections/tlsconfig"
    14  	"github.com/xiaobinqt/libcompose/version"
    15  )
    16  
    17  const (
    18  	// DefaultAPIVersion is the default docker API version set by libcompose
    19  	DefaultAPIVersion   = "v1.20"
    20  	defaultTrustKeyFile = "key.json"
    21  	defaultCaFile       = "ca.pem"
    22  	defaultKeyFile      = "key.pem"
    23  	defaultCertFile     = "cert.pem"
    24  )
    25  
    26  var (
    27  	dockerCertPath = os.Getenv("DOCKER_CERT_PATH")
    28  )
    29  
    30  func init() {
    31  	if dockerCertPath == "" {
    32  		dockerCertPath = cliconfig.Dir()
    33  	}
    34  }
    35  
    36  // Options holds docker client options (host, tls, ..)
    37  type Options struct {
    38  	TLS        bool
    39  	TLSVerify  bool
    40  	TLSOptions tlsconfig.Options
    41  	TrustKey   string
    42  	Host       string
    43  	APIVersion string
    44  }
    45  
    46  // Create creates a docker client based on the specified options.
    47  func Create(c Options) (client.APIClient, error) {
    48  	if c.Host == "" {
    49  		if os.Getenv("DOCKER_API_VERSION") == "" {
    50  			os.Setenv("DOCKER_API_VERSION", DefaultAPIVersion)
    51  		}
    52  		client, err := client.NewEnvClient()
    53  		if err != nil {
    54  			return nil, err
    55  		}
    56  		return client, nil
    57  	}
    58  
    59  	apiVersion := c.APIVersion
    60  	if apiVersion == "" {
    61  		apiVersion = DefaultAPIVersion
    62  	}
    63  
    64  	if c.TLSOptions.CAFile == "" {
    65  		c.TLSOptions.CAFile = filepath.Join(dockerCertPath, defaultCaFile)
    66  	}
    67  	if c.TLSOptions.CertFile == "" {
    68  		c.TLSOptions.CertFile = filepath.Join(dockerCertPath, defaultCertFile)
    69  	}
    70  	if c.TLSOptions.KeyFile == "" {
    71  		c.TLSOptions.KeyFile = filepath.Join(dockerCertPath, defaultKeyFile)
    72  	}
    73  	if c.TrustKey == "" {
    74  		c.TrustKey = filepath.Join(homedir.Get(), ".docker", defaultTrustKeyFile)
    75  	}
    76  	if c.TLSVerify {
    77  		c.TLS = true
    78  	}
    79  	if c.TLS {
    80  		c.TLSOptions.InsecureSkipVerify = !c.TLSVerify
    81  	}
    82  
    83  	var httpClient *http.Client
    84  	if c.TLS {
    85  		config, err := tlsconfig.Client(c.TLSOptions)
    86  		if err != nil {
    87  			return nil, err
    88  		}
    89  
    90  		httpClient = &http.Client{
    91  			Transport: &http.Transport{
    92  				TLSClientConfig: config,
    93  			},
    94  		}
    95  	}
    96  
    97  	customHeaders := map[string]string{}
    98  	customHeaders["User-Agent"] = fmt.Sprintf("Libcompose-Client/%s (%s)", version.VERSION, runtime.GOOS)
    99  
   100  	client, err := client.NewClientWithOpts(
   101  		client.WithHTTPClient(httpClient),
   102  		client.WithHost(c.Host),
   103  		client.WithVersion(apiVersion),
   104  		client.WithHTTPHeaders(customHeaders),
   105  	)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  	return client, nil
   110  }