Skip to content

Commit 05f90d1

Browse files
committed
set default schema to tcp for docker host
Signed-off-by: Lifubang <lifubang@acmcoder.com>
1 parent b9a1a21 commit 05f90d1

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

cli/command/cli.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,23 +274,23 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, isTrusted bool, containe
274274

275275
// NewAPIClientFromFlags creates a new APIClient from command line flags
276276
func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
277-
unparsedHost, err := getUnparsedServerHost(opts.Hosts)
278-
if err != nil {
279-
return &client.Client{}, err
280-
}
281277
var clientOpts []func(*client.Client) error
282-
helper, err := connhelper.GetConnectionHelper(unparsedHost)
283-
if err != nil {
284-
return &client.Client{}, err
285-
}
286-
if helper == nil {
278+
host, errp := getServerHost(opts.Hosts, opts.TLSOptions)
279+
if errp == nil {
287280
clientOpts = append(clientOpts, withHTTPClient(opts.TLSOptions))
288-
host, err := dopts.ParseHost(opts.TLSOptions != nil, unparsedHost)
281+
clientOpts = append(clientOpts, client.WithHost(host))
282+
} else {
283+
unparsedHost, err := getUnparsedServerHost(opts.Hosts)
289284
if err != nil {
290285
return &client.Client{}, err
291286
}
292-
clientOpts = append(clientOpts, client.WithHost(host))
293-
} else {
287+
helper, err := connhelper.GetConnectionHelper(unparsedHost)
288+
if err != nil {
289+
return &client.Client{}, err
290+
}
291+
if helper == nil {
292+
return &client.Client{}, errp
293+
}
294294
clientOpts = append(clientOpts, func(c *client.Client) error {
295295
httpClient := &http.Client{
296296
// No tls
@@ -321,6 +321,20 @@ func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.
321321
return client.NewClientWithOpts(clientOpts...)
322322
}
323323

324+
func getServerHost(hosts []string, tlsOptions *tlsconfig.Options) (string, error) {
325+
var host string
326+
switch len(hosts) {
327+
case 0:
328+
host = os.Getenv("DOCKER_HOST")
329+
case 1:
330+
host = hosts[0]
331+
default:
332+
return "", errors.New("Please specify only one -H")
333+
}
334+
335+
return dopts.ParseHost(tlsOptions != nil, host)
336+
}
337+
324338
func getUnparsedServerHost(hosts []string) (string, error) {
325339
var host string
326340
switch len(hosts) {

0 commit comments

Comments
 (0)