|
| 1 | +package build |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + |
| 7 | + "github.com/docker/cli/cli/command" |
| 8 | + "github.com/docker/cli/cli/config" |
| 9 | + cliflags "github.com/docker/cli/cli/flags" |
| 10 | + "github.com/docker/docker/client" |
| 11 | + "github.com/docker/go-connections/tlsconfig" |
| 12 | +) |
| 13 | + |
| 14 | +// Copied from github.com/docker/cli/cli/flags/common.go |
| 15 | + |
| 16 | +var ( |
| 17 | + dockerCertPath = os.Getenv(client.EnvOverrideCertPath) |
| 18 | + dockerTLSVerify = os.Getenv(client.EnvTLSVerify) != "" |
| 19 | + dockerTLS = os.Getenv("DOCKER_TLS") != "" |
| 20 | +) |
| 21 | + |
| 22 | +func newDockerCLI() (*command.DockerCli, error) { |
| 23 | + dockerCli, err := command.NewDockerCli() |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + |
| 28 | + // Construct options with TLS |
| 29 | + opts := cliflags.NewClientOptions() |
| 30 | + if dockerCertPath == "" { |
| 31 | + dockerCertPath = config.Dir() |
| 32 | + } |
| 33 | + |
| 34 | + opts.Common.TLS = dockerTLS |
| 35 | + opts.Common.TLSVerify = dockerTLSVerify |
| 36 | + if opts.Common.TLSVerify { |
| 37 | + opts.Common.TLS = true |
| 38 | + } |
| 39 | + if opts.Common.TLS { |
| 40 | + opts.Common.TLSOptions = &tlsconfig.Options{ |
| 41 | + CAFile: filepath.Join(dockerCertPath, cliflags.DefaultCaFile), |
| 42 | + CertFile: filepath.Join(dockerCertPath, cliflags.DefaultCertFile), |
| 43 | + KeyFile: filepath.Join(dockerCertPath, cliflags.DefaultKeyFile), |
| 44 | + InsecureSkipVerify: !opts.Common.TLSVerify, |
| 45 | + } |
| 46 | + // Reset CertFile and KeyFile to empty string if the respective default files were not found. |
| 47 | + if _, err := os.Stat(opts.Common.TLSOptions.CertFile); os.IsNotExist(err) { |
| 48 | + opts.Common.TLSOptions.CertFile = "" |
| 49 | + } |
| 50 | + if _, err := os.Stat(opts.Common.TLSOptions.KeyFile); os.IsNotExist(err) { |
| 51 | + opts.Common.TLSOptions.KeyFile = "" |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + err = dockerCli.Initialize(opts) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + return dockerCli, err |
| 61 | +} |
0 commit comments