Skip to content

Commit 7bec8d4

Browse files
committed
system/info: failure to connect to docker socket should propagate error
Signed-off-by: Alano Terblanche <[email protected]>
1 parent 2d74733 commit 7bec8d4

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

cli/command/system/info.go

+22-17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/docker/cli/templates"
2323
"github.com/docker/docker/api/types/swarm"
2424
"github.com/docker/docker/api/types/system"
25+
"github.com/docker/docker/client"
2526
"github.com/docker/docker/registry"
2627
"github.com/docker/go-units"
2728
"github.com/spf13/cobra"
@@ -89,37 +90,41 @@ func runInfo(ctx context.Context, cmd *cobra.Command, dockerCli command.Cli, opt
8990
clientVersion: newClientVersion(dockerCli.CurrentContext(), nil),
9091
Debug: debug.IsEnabled(),
9192
},
92-
Info: &system.Info{},
93+
Info: nil,
9394
}
9495
if plugins, err := pluginmanager.ListPlugins(dockerCli, cmd.Root()); err == nil {
9596
info.ClientInfo.Plugins = plugins
9697
} else {
9798
info.ClientErrors = append(info.ClientErrors, err.Error())
9899
}
99100

101+
noFormat := opts.format == ""
102+
103+
if noFormat {
104+
info.UserName = dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username
105+
info.ClientInfo.APIVersion = dockerCli.CurrentVersion()
106+
}
107+
100108
if needsServerInfo(opts.format, info) {
101-
if dinfo, err := dockerCli.Client().Info(ctx); err == nil {
102-
info.Info = &dinfo
103-
} else {
104-
info.ServerErrors = append(info.ServerErrors, err.Error())
105-
if opts.format == "" {
106-
// reset the server info to prevent printing "empty" Server info
107-
// and warnings, but don't reset it if a custom format was specified
108-
// to prevent errors from Go's template parsing during format.
109-
info.Info = nil
110-
} else {
111-
// if a format is provided, print the error, as it may be hidden
112-
// otherwise if the template doesn't include the ServerErrors field.
113-
fprintln(dockerCli.Err(), err)
109+
dinfo, err := dockerCli.Client().Info(ctx)
110+
if err != nil && client.IsErrConnectionFailed(err) {
111+
var printErr error
112+
if noFormat {
113+
printErr = prettyPrintInfo(dockerCli, info)
114114
}
115+
return cli.StatusError{StatusCode: 125, Cause: errors.Join(err, printErr)}
116+
} else if err != nil {
117+
// if a format is provided, print the error, as it may be hidden
118+
// otherwise if the template doesn't include the ServerErrors field.
119+
info.ServerErrors = append(info.ServerErrors, err.Error())
115120
}
121+
info.Info = &dinfo
116122
}
117123

118-
if opts.format == "" {
119-
info.UserName = dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username
120-
info.ClientInfo.APIVersion = dockerCli.CurrentVersion()
124+
if noFormat {
121125
return prettyPrintInfo(dockerCli, info)
122126
}
127+
123128
return formatInfo(dockerCli.Out(), info, opts.format)
124129
}
125130

0 commit comments

Comments
 (0)