@@ -22,6 +22,7 @@ import (
22
22
"github.com/docker/cli/templates"
23
23
"github.com/docker/docker/api/types/swarm"
24
24
"github.com/docker/docker/api/types/system"
25
+ "github.com/docker/docker/client"
25
26
"github.com/docker/docker/registry"
26
27
"github.com/docker/go-units"
27
28
"github.com/spf13/cobra"
@@ -89,37 +90,32 @@ func runInfo(ctx context.Context, cmd *cobra.Command, dockerCli command.Cli, opt
89
90
clientVersion : newClientVersion (dockerCli .CurrentContext (), nil ),
90
91
Debug : debug .IsEnabled (),
91
92
},
92
- Info : & system. Info {} ,
93
+ Info : nil ,
93
94
}
94
95
if plugins , err := pluginmanager .ListPlugins (dockerCli , cmd .Root ()); err == nil {
95
96
info .ClientInfo .Plugins = plugins
96
97
} else {
97
98
info .ClientErrors = append (info .ClientErrors , err .Error ())
98
99
}
99
100
101
+ if opts .format == "" {
102
+ info .UserName = dockerCli .ConfigFile ().AuthConfigs [registry .IndexServer ].Username
103
+ info .ClientInfo .APIVersion = dockerCli .CurrentVersion ()
104
+ }
105
+
100
106
if needsServerInfo (opts .format , info ) {
101
- if dinfo , err := dockerCli .Client ().Info (ctx ); err == nil {
102
- info .Info = & dinfo
103
- } else {
107
+ dinfo , err := dockerCli .Client ().Info (ctx )
108
+ if err != nil && client .IsErrConnectionFailed (err ) {
109
+ printErr := prettyPrintInfo (dockerCli , info )
110
+ return cli.StatusError {StatusCode : 125 , Cause : errors .Join (err , printErr )}
111
+ } else if err != nil {
112
+ // if a format is provided, print the error, as it may be hidden
113
+ // otherwise if the template doesn't include the ServerErrors field.
104
114
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 )
114
- }
115
115
}
116
+ info .Info = & dinfo
116
117
}
117
118
118
- if opts .format == "" {
119
- info .UserName = dockerCli .ConfigFile ().AuthConfigs [registry .IndexServer ].Username
120
- info .ClientInfo .APIVersion = dockerCli .CurrentVersion ()
121
- return prettyPrintInfo (dockerCli , info )
122
- }
123
119
return formatInfo (dockerCli .Out (), info , opts .format )
124
120
}
125
121
0 commit comments