needsServerInfo detects if the given template uses any server information. If only client-side information is used in the template, we can skip connecting to the daemon. This allows (e.g.) to only get cli-plugin information, without also making a (potentially expensive) API call.
(template string, info dockerInfo)
| 150 | // connecting to the daemon. This allows (e.g.) to only get cli-plugin |
| 151 | // information, without also making a (potentially expensive) API call. |
| 152 | func needsServerInfo(template string, info dockerInfo) bool { |
| 153 | switch template { |
| 154 | case "", formatter.JSONFormat, formatter.JSONFormatKey: |
| 155 | // No format (default) and full JSON output need server-side info. |
| 156 | return true |
| 157 | default: |
| 158 | if placeHolders.FindString(template) == "" { |
| 159 | // The template does not contain formatting fields; assume we |
| 160 | // need server-side information to render it, and return early. |
| 161 | return true |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // A template is provided and has at least one field set. |
| 166 | tmpl, err := templates.Parse(template) |
| 167 | if err != nil { |
| 168 | // ignore parsing errors here, and let regular code handle them |
| 169 | return true |
| 170 | } |
| 171 | |
| 172 | type sparseInfo struct { |
| 173 | ClientInfo *clientInfo `json:",omitempty"` |
| 174 | ClientErrors []string `json:",omitempty"` |
| 175 | } |
| 176 | |
| 177 | // This constructs an "info" object that only has the client-side fields. |
| 178 | err = tmpl.Execute(io.Discard, sparseInfo{ |
| 179 | ClientInfo: info.ClientInfo, |
| 180 | ClientErrors: info.ClientErrors, |
| 181 | }) |
| 182 | // If executing the template failed, it means the template needs |
| 183 | // server-side information as well. If it succeeded without server-side |
| 184 | // information, we don't need to make API calls to collect that information. |
| 185 | return err != nil |
| 186 | } |
| 187 | |
| 188 | func prettyPrintInfo(streams command.Streams, info dockerInfo) error { |
| 189 | // Only append the platform info if it's not empty, to prevent printing a trailing space. |
searching dependent graphs…