cli/command/system: needsServerInfo: add fast-paths

We can return early without executing the regular expression or evaluating
the template for `--format=json` or `--format='{{json .}}'`.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-04-10 10:40:34 +02:00
parent ac375caa87
commit 932574363f
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -149,11 +149,16 @@ var placeHolders = regexp.MustCompile(`\.[a-zA-Z]`)
// connecting to the daemon. This allows (e.g.) to only get cli-plugin
// information, without also making a (potentially expensive) API call.
func needsServerInfo(template string, info dockerInfo) bool {
if len(template) == 0 || placeHolders.FindString(template) == "" {
// The template is empty, or does not contain formatting fields
// (e.g. `table` or `raw` or `{{ json .}}`). Assume we need server-side
// information to render it.
switch template {
case "", formatter.JSONFormat, formatter.JSONFormatKey:
// No format (default) and full JSON output need server-side info.
return true
default:
if placeHolders.FindString(template) == "" {
// The template does not contain formatting fields; assume we
// need server-side information to render it, and return early.
return true
}
}
// A template is provided and has at least one field set.