From e96e17d1029dc5768ecf6bfb0da2f8aae1faa7f4 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 6 May 2022 16:15:20 +0200 Subject: [PATCH] info: improve handling of empty Info Before this change, the function could print an error in some cases, for example; ```bash docker -H tcp://127.0.0.1:2375 info --format '{{.LoggingDriver}}' template: :1:2: executing "" at <.LoggingDriver>: reflect: indirection through nil pointer to embedded struct field Info ``` With this patch applied, the error is handled gracefully, and when failing to connect with the daemon, the error is logged; ```bash docker -H tcp://127.0.0.1:2375 info --format '{{.LoggingDriver}}' Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? docker -H tcp://127.0.0.1:2375 info --format '{{json .}}' Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? {"ID":"","Containers":0,"..."}} ``` Note that the connection error is also included in the JSON `ServerErrors` field, so that the information does not get lost, even if STDERR would be redirected; ```bash docker -H tcp://127.0.0.1:2375 info --format '{{json .ServerErrors}}' 2> /dev/null ["Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running?"] ``` Signed-off-by: Sebastiaan van Stijn --- cli/command/system/info.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cli/command/system/info.go b/cli/command/system/info.go index ff2f794221..f3ce01f861 100644 --- a/cli/command/system/info.go +++ b/cli/command/system/info.go @@ -67,11 +67,12 @@ func NewInfoCommand(dockerCli command.Cli) *cobra.Command { } func runInfo(cmd *cobra.Command, dockerCli command.Cli, opts *infoOptions) error { - var info info - - info.ClientInfo = &clientInfo{ - Context: dockerCli.CurrentContext(), - Debug: debug.IsEnabled(), + info := info{ + ClientInfo: &clientInfo{ + Context: dockerCli.CurrentContext(), + Debug: debug.IsEnabled(), + }, + Info: &types.Info{}, } if plugins, err := pluginmanager.ListPlugins(dockerCli, cmd.Root()); err == nil { info.ClientInfo.Plugins = plugins @@ -84,6 +85,7 @@ func runInfo(cmd *cobra.Command, dockerCli command.Cli, opts *infoOptions) error if dinfo, err := dockerCli.Client().Info(ctx); err == nil { info.Info = &dinfo } else { + fmt.Fprintln(dockerCli.Err(), err) info.ServerErrors = append(info.ServerErrors, err.Error()) } }