@@ -5,31 +5,17 @@ import (
55 "fmt"
66 "io"
77 "log/slog"
8- "maps"
9- "path/filepath"
108 "time"
119
12- "github.com/docker/docker/client"
13- dockercontext "github.com/docker/go-sdk/context"
14- )
15-
16- const (
17- // Headers used for docker client requests.
18- headerUserAgent = "User-Agent"
19-
20- // TLS certificate files.
21- tlsCACertFile = "ca.pem"
22- tlsCertFile = "cert.pem"
23- tlsKeyFile = "key.pem"
10+ "github.com/docker/cli/cli/command"
11+ "github.com/docker/cli/cli/flags"
2412)
2513
2614var (
2715 defaultLogger = slog .New (slog .NewTextHandler (io .Discard , nil ))
2816
2917 defaultUserAgent = "docker-go-sdk/" + Version ()
3018
31- defaultOpts = []client.Opt {client .FromEnv , client .WithAPIVersionNegotiation ()}
32-
3319 defaultHealthCheck = func (ctx context.Context ) func (c SDKClient ) error {
3420 return func (c SDKClient ) error {
3521 var pingErr error
@@ -71,112 +57,28 @@ func New(ctx context.Context, options ...ClientOption) (SDKClient, error) {
7157 log : defaultLogger ,
7258 healthCheck : defaultHealthCheck ,
7359 }
74- for _ , opt := range options {
75- if err := opt .Apply (c ); err != nil {
76- return nil , fmt .Errorf ("apply option: %w" , err )
77- }
78- }
7960
80- if err := c .init (); err != nil {
81- return nil , fmt .Errorf ("load config: %w" , err )
82- }
83-
84- if err := c .healthCheck (ctx )(c ); err != nil {
85- return nil , fmt .Errorf ("health check: %w" , err )
86- }
87-
88- return c , nil
89- }
90-
91- // init initializes the client.
92- // This method is safe for concurrent use by multiple goroutines.
93- func (c * sdkClient ) init () error {
94- if c .APIClient != nil || c .err != nil {
95- return c .err
96- }
97-
98- // Set the default values for the client:
99- // - log
100- // - dockerHost
101- // - currentContext
102- if c .err = c .defaultValues (); c .err != nil {
103- return fmt .Errorf ("default values: %w" , c .err )
104- }
105-
106- if c .cfg , c .err = newConfig (c .dockerHost ); c .err != nil {
107- return c .err
108- }
109-
110- opts := make ([]client.Opt , len (defaultOpts ), len (defaultOpts )+ len (c .dockerOpts ))
111- copy (opts , defaultOpts )
112-
113- // Add all collected Docker options
114- opts = append (opts , c .dockerOpts ... )
115-
116- if c .cfg .TLSVerify {
117- // For further information see:
118- // https://docs.docker.com/engine/security/protect-access/#use-tls-https-to-protect-the-docker-daemon-socket
119- opts = append (opts , client .WithTLSClientConfig (
120- filepath .Join (c .cfg .CertPath , tlsCACertFile ),
121- filepath .Join (c .cfg .CertPath , tlsCertFile ),
122- filepath .Join (c .cfg .CertPath , tlsKeyFile ),
123- ))
124- }
125- if c .cfg .Host != "" {
126- // apply the host from the config if it is set
127- opts = append (opts , client .WithHost (c .cfg .Host ))
128- }
129-
130- httpHeaders := make (map [string ]string )
131- maps .Copy (httpHeaders , c .extraHeaders )
132-
133- // Append the SDK headers last.
134- httpHeaders [headerUserAgent ] = defaultUserAgent
135-
136- opts = append (opts , client .WithHTTPHeaders (httpHeaders ))
137-
138- api , err := client .NewClientWithOpts (opts ... )
61+ cli , err := command .NewDockerCli (command .WithUserAgent (defaultUserAgent ))
13962 if err != nil {
140- return fmt . Errorf ( "new client: %w" , err )
63+ return nil , err
14164 }
142- c .APIClient = api
143- return nil
144- }
14565
146- // defaultValues sets the default values for the client.
147- // If no logger is provided, the default one is used.
148- // If no docker host is provided and no docker context is provided, the current docker host and context are used.
149- // If no docker host is provided but a docker context is provided, the docker host from the context is used.
150- // If a docker host is provided, it is used as is.
151- func (c * sdkClient ) defaultValues () error {
152- if c .log == nil {
153- c .log = defaultLogger
66+ err = cli .Initialize (flags .NewClientOptions ())
67+ if err != nil {
68+ return nil , err
15469 }
70+ c .APIClient = cli .Client ()
71+ c .config = cli .ConfigFile ()
15572
156- if c .dockerHost == "" && c .dockerContext == "" {
157- currentDockerHost , err := dockercontext .CurrentDockerHost ()
158- if err != nil {
159- return fmt .Errorf ("current docker host: %w" , err )
160- }
161- currentContext , err := dockercontext .Current ()
162- if err != nil {
163- return fmt .Errorf ("current context: %w" , err )
73+ for _ , opt := range options {
74+ if err := opt .Apply (c ); err != nil {
75+ return nil , fmt .Errorf ("apply option: %w" , err )
16476 }
165-
166- c .dockerHost = currentDockerHost
167- c .dockerContext = currentContext
168-
169- return nil
17077 }
17178
172- if c .dockerContext != "" {
173- dockerHost , err := dockercontext .DockerHostFromContext (c .dockerContext )
174- if err != nil {
175- return fmt .Errorf ("docker host from context: %w" , err )
176- }
177-
178- c .dockerHost = dockerHost
79+ if err := c .healthCheck (ctx )(c ); err != nil {
80+ return nil , fmt .Errorf ("health check: %w" , err )
17981 }
18082
181- return nil
83+ return c , nil
18284}
0 commit comments