cli/command/system: prettyPrintServerInfo: move out collecting username

Make this function only _print_ the info we have, and not read the username
from the credential-store.

This patch adds a Username field to the (local) `info` type, and sets it
when needed, so that prettyPrintServerInfo only has to format and print
the information, instead of calling out to the credential-store.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-04-14 00:25:08 +02:00
parent 71fde20e17
commit be307c5792
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 7 additions and 8 deletions

View File

@ -42,6 +42,7 @@ type info struct {
// object. // object.
*types.Info `json:",omitempty"` *types.Info `json:",omitempty"`
ServerErrors []string `json:",omitempty"` ServerErrors []string `json:",omitempty"`
UserName string `json:"-"`
ClientInfo *clientInfo `json:",omitempty"` ClientInfo *clientInfo `json:",omitempty"`
ClientErrors []string `json:",omitempty"` ClientErrors []string `json:",omitempty"`
@ -113,6 +114,7 @@ func runInfo(cmd *cobra.Command, dockerCli command.Cli, opts *infoOptions) error
} }
if opts.format == "" { if opts.format == "" {
info.UserName = dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username
return prettyPrintInfo(dockerCli, info) return prettyPrintInfo(dockerCli, info)
} }
return formatInfo(dockerCli, info, opts.format) return formatInfo(dockerCli, info, opts.format)
@ -174,7 +176,7 @@ func prettyPrintInfo(dockerCli command.Cli, info info) error {
fmt.Fprintln(dockerCli.Out()) fmt.Fprintln(dockerCli.Out())
fmt.Fprintln(dockerCli.Out(), "Server:") fmt.Fprintln(dockerCli.Out(), "Server:")
if info.Info != nil { if info.Info != nil {
for _, err := range prettyPrintServerInfo(dockerCli, *info.Info) { for _, err := range prettyPrintServerInfo(dockerCli, &info) {
info.ServerErrors = append(info.ServerErrors, err.Error()) info.ServerErrors = append(info.ServerErrors, err.Error())
} }
} }
@ -212,7 +214,7 @@ func prettyPrintClientInfo(dockerCli command.Cli, info clientInfo) {
} }
//nolint:gocyclo //nolint:gocyclo
func prettyPrintServerInfo(dockerCli command.Cli, info types.Info) []error { func prettyPrintServerInfo(dockerCli command.Cli, info *info) []error {
var errs []error var errs []error
fmt.Fprintln(dockerCli.Out(), " Containers:", info.Containers) fmt.Fprintln(dockerCli.Out(), " Containers:", info.Containers)
@ -247,7 +249,7 @@ func prettyPrintServerInfo(dockerCli command.Cli, info types.Info) []error {
fmt.Fprintln(dockerCli.Out(), " Log:", strings.Join(info.Plugins.Log, " ")) fmt.Fprintln(dockerCli.Out(), " Log:", strings.Join(info.Plugins.Log, " "))
fmt.Fprintln(dockerCli.Out(), " Swarm:", info.Swarm.LocalNodeState) fmt.Fprintln(dockerCli.Out(), " Swarm:", info.Swarm.LocalNodeState)
printSwarmInfo(dockerCli, info) printSwarmInfo(dockerCli, *info.Info)
if len(info.Runtimes) > 0 { if len(info.Runtimes) > 0 {
fmt.Fprint(dockerCli.Out(), " Runtimes:") fmt.Fprint(dockerCli.Out(), " Runtimes:")
@ -319,10 +321,7 @@ func prettyPrintServerInfo(dockerCli command.Cli, info types.Info) []error {
fprintlnNonEmpty(dockerCli.Out(), " HTTP Proxy:", info.HTTPProxy) fprintlnNonEmpty(dockerCli.Out(), " HTTP Proxy:", info.HTTPProxy)
fprintlnNonEmpty(dockerCli.Out(), " HTTPS Proxy:", info.HTTPSProxy) fprintlnNonEmpty(dockerCli.Out(), " HTTPS Proxy:", info.HTTPSProxy)
fprintlnNonEmpty(dockerCli.Out(), " No Proxy:", info.NoProxy) fprintlnNonEmpty(dockerCli.Out(), " No Proxy:", info.NoProxy)
fprintlnNonEmpty(dockerCli.Out(), " Username:", info.UserName)
u := dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username
fprintlnNonEmpty(dockerCli.Out(), " Username:", u)
if len(info.Labels) > 0 { if len(info.Labels) > 0 {
fmt.Fprintln(dockerCli.Out(), " Labels:") fmt.Fprintln(dockerCli.Out(), " Labels:")
for _, lbl := range info.Labels { for _, lbl := range info.Labels {
@ -367,7 +366,7 @@ func prettyPrintServerInfo(dockerCli command.Cli, info types.Info) []error {
fmt.Fprint(dockerCli.Out(), "\n") fmt.Fprint(dockerCli.Out(), "\n")
printServerWarnings(dockerCli, info) printServerWarnings(dockerCli, *info.Info)
return errs return errs
} }