From 9dc54f3fbe5ff517bc0298aec56f9a6407cce74d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 10 May 2022 12:13:03 +0200 Subject: [PATCH] info: don't print server info if we failed to connect Before this patch, the Server output would be printed even if we failed to connect (including WARNINGS): ```bash docker -H tcp://127.0.0.1:2375 info Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? Client: Context: default Debug Mode: false Plugins: buildx: Docker Buildx (Docker Inc., v0.8.2) compose: Docker Compose (Docker Inc., v2.4.1) sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0) scan: Docker Scan (Docker Inc., v0.17.0) Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Plugins: Volume: Network: Log: Swarm: NodeID: Is Manager: false Node Address: CPUs: 0 Total Memory: 0B Docker Root Dir: Debug Mode: false Experimental: false Live Restore Enabled: false WARNING: No memory limit support WARNING: No swap limit support WARNING: No oom kill disable support WARNING: No cpu cfs quota support WARNING: No cpu cfs period support WARNING: No cpu shares support WARNING: No cpuset support WARNING: IPv4 forwarding is disabled WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled ERROR: Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? errors pretty printing info ``` With this patch; ```bash docker -H tcp://127.0.0.1:2375 info Client: Context: default Debug Mode: false Plugins: buildx: Docker Buildx (Docker Inc., v0.8.2) compose: Docker Compose (Docker Inc., v2.4.1) sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0) scan: Docker Scan (Docker Inc., v0.17.0) Server: ERROR: Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? errors pretty printing info ``` And if a custom format is used: ```bash docker -H tcp://127.0.0.1:2375 info --format '{{.Containers}}' Cannot connect to the Docker daemon at tcp://127.0.0.1:2375. Is the docker daemon running? 0 ``` Signed-off-by: Sebastiaan van Stijn --- cli/command/system/info.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cli/command/system/info.go b/cli/command/system/info.go index f3ce01f861..2a412c99fc 100644 --- a/cli/command/system/info.go +++ b/cli/command/system/info.go @@ -85,8 +85,17 @@ 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()) + if opts.format == "" { + // reset the server info to prevent printing "empty" Server info + // and warnings, but don't reset it if a custom format was specified + // to prevent errors from Go's template parsing during format. + info.Info = nil + } else { + // if a format is provided, print the error, as it may be hidden + // otherwise if the template doesn't include the ServerErrors field. + fmt.Fprintln(dockerCli.Err(), err) + } } }