From ce3090ccc476f47a132295336c2060dd8ed26f66 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 9 Mar 2025 22:20:12 +0100 Subject: [PATCH] cli/command: move PrettyPrint utility to cli/command/formatter This utility was only used internally, and has no external consumers; move it to the "formatter" package, which is also imported in all files using this utility. Signed-off-by: Sebastiaan van Stijn --- cli/command/config/formatter.go | 5 ++--- cli/command/formatter/displayutils.go | 26 ++++++++++++++++++++++++++ cli/command/node/formatter.go | 17 ++++++++--------- cli/command/secret/formatter.go | 5 ++--- cli/command/task/formatter.go | 5 ++--- cli/command/utils.go | 24 ------------------------ 6 files changed, 40 insertions(+), 42 deletions(-) diff --git a/cli/command/config/formatter.go b/cli/command/config/formatter.go index 4f69f880b0..42072d7580 100644 --- a/cli/command/config/formatter.go +++ b/cli/command/config/formatter.go @@ -5,7 +5,6 @@ import ( "strings" "time" - "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/cli/command/inspect" "github.com/docker/docker/api/types/swarm" @@ -157,11 +156,11 @@ func (ctx *configInspectContext) Labels() map[string]string { } func (ctx *configInspectContext) CreatedAt() string { - return command.PrettyPrint(ctx.Config.CreatedAt) + return formatter.PrettyPrint(ctx.Config.CreatedAt) } func (ctx *configInspectContext) UpdatedAt() string { - return command.PrettyPrint(ctx.Config.UpdatedAt) + return formatter.PrettyPrint(ctx.Config.UpdatedAt) } func (ctx *configInspectContext) Data() string { diff --git a/cli/command/formatter/displayutils.go b/cli/command/formatter/displayutils.go index 7847bb307e..0f7267e717 100644 --- a/cli/command/formatter/displayutils.go +++ b/cli/command/formatter/displayutils.go @@ -1,6 +1,8 @@ package formatter import ( + "fmt" + "strings" "unicode/utf8" "golang.org/x/text/width" @@ -59,3 +61,27 @@ func Ellipsis(s string, maxDisplayWidth int) string { } return s } + +// capitalizeFirst capitalizes the first character of string +func capitalizeFirst(s string) string { + switch l := len(s); l { + case 0: + return s + case 1: + return strings.ToLower(s) + default: + return strings.ToUpper(string(s[0])) + strings.ToLower(s[1:]) + } +} + +// PrettyPrint outputs arbitrary data for human formatted output by uppercasing the first letter. +func PrettyPrint(i any) string { + switch t := i.(type) { + case nil: + return "None" + case string: + return capitalizeFirst(t) + default: + return capitalizeFirst(fmt.Sprintf("%s", t)) + } +} diff --git a/cli/command/node/formatter.go b/cli/command/node/formatter.go index d625b485e1..19c3530bb0 100644 --- a/cli/command/node/formatter.go +++ b/cli/command/node/formatter.go @@ -6,7 +6,6 @@ import ( "reflect" "strings" - "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/cli/command/inspect" "github.com/docker/docker/api/types/swarm" @@ -147,11 +146,11 @@ func (c *nodeContext) Hostname() string { } func (c *nodeContext) Status() string { - return command.PrettyPrint(string(c.n.Status.State)) + return formatter.PrettyPrint(string(c.n.Status.State)) } func (c *nodeContext) Availability() string { - return command.PrettyPrint(string(c.n.Spec.Availability)) + return formatter.PrettyPrint(string(c.n.Spec.Availability)) } func (c *nodeContext) ManagerStatus() string { @@ -163,7 +162,7 @@ func (c *nodeContext) ManagerStatus() string { reachability = string(c.n.ManagerStatus.Reachability) } } - return command.PrettyPrint(reachability) + return formatter.PrettyPrint(reachability) } func (c *nodeContext) TLSStatus() string { @@ -226,11 +225,11 @@ func (ctx *nodeInspectContext) Hostname() string { } func (ctx *nodeInspectContext) CreatedAt() string { - return command.PrettyPrint(ctx.Node.CreatedAt) + return formatter.PrettyPrint(ctx.Node.CreatedAt) } func (ctx *nodeInspectContext) StatusState() string { - return command.PrettyPrint(ctx.Node.Status.State) + return formatter.PrettyPrint(ctx.Node.Status.State) } func (ctx *nodeInspectContext) HasStatusMessage() bool { @@ -238,11 +237,11 @@ func (ctx *nodeInspectContext) HasStatusMessage() bool { } func (ctx *nodeInspectContext) StatusMessage() string { - return command.PrettyPrint(ctx.Node.Status.Message) + return formatter.PrettyPrint(ctx.Node.Status.Message) } func (ctx *nodeInspectContext) SpecAvailability() string { - return command.PrettyPrint(ctx.Node.Spec.Availability) + return formatter.PrettyPrint(ctx.Node.Spec.Availability) } func (ctx *nodeInspectContext) HasStatusAddr() bool { @@ -262,7 +261,7 @@ func (ctx *nodeInspectContext) ManagerStatusAddr() string { } func (ctx *nodeInspectContext) ManagerStatusReachability() string { - return command.PrettyPrint(ctx.Node.ManagerStatus.Reachability) + return formatter.PrettyPrint(ctx.Node.ManagerStatus.Reachability) } func (ctx *nodeInspectContext) IsManagerStatusLeader() bool { diff --git a/cli/command/secret/formatter.go b/cli/command/secret/formatter.go index a950478431..2883bfcf4c 100644 --- a/cli/command/secret/formatter.go +++ b/cli/command/secret/formatter.go @@ -5,7 +5,6 @@ import ( "strings" "time" - "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/cli/command/inspect" "github.com/docker/docker/api/types/swarm" @@ -171,9 +170,9 @@ func (ctx *secretInspectContext) Driver() string { } func (ctx *secretInspectContext) CreatedAt() string { - return command.PrettyPrint(ctx.Secret.CreatedAt) + return formatter.PrettyPrint(ctx.Secret.CreatedAt) } func (ctx *secretInspectContext) UpdatedAt() string { - return command.PrettyPrint(ctx.Secret.UpdatedAt) + return formatter.PrettyPrint(ctx.Secret.UpdatedAt) } diff --git a/cli/command/task/formatter.go b/cli/command/task/formatter.go index 77748ab52e..0ce03d6691 100644 --- a/cli/command/task/formatter.go +++ b/cli/command/task/formatter.go @@ -6,7 +6,6 @@ import ( "time" "github.com/distribution/reference" - "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/formatter" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/pkg/stringid" @@ -110,12 +109,12 @@ func (c *taskContext) Node() string { } func (c *taskContext) DesiredState() string { - return command.PrettyPrint(c.task.DesiredState) + return formatter.PrettyPrint(c.task.DesiredState) } func (c *taskContext) CurrentState() string { return fmt.Sprintf("%s %s ago", - command.PrettyPrint(c.task.Status.State), + formatter.PrettyPrint(c.task.Status.State), strings.ToLower(units.HumanDuration(time.Since(c.task.Status.Timestamp))), ) } diff --git a/cli/command/utils.go b/cli/command/utils.go index 8a8368fbaa..a7a9da8fda 100644 --- a/cli/command/utils.go +++ b/cli/command/utils.go @@ -51,30 +51,6 @@ func CopyToFile(outfile string, r io.Reader) error { return nil } -// capitalizeFirst capitalizes the first character of string -func capitalizeFirst(s string) string { - switch l := len(s); l { - case 0: - return s - case 1: - return strings.ToLower(s) - default: - return strings.ToUpper(string(s[0])) + strings.ToLower(s[1:]) - } -} - -// PrettyPrint outputs arbitrary data for human formatted output by uppercasing the first letter. -func PrettyPrint(i any) string { - switch t := i.(type) { - case nil: - return "None" - case string: - return capitalizeFirst(t) - default: - return capitalizeFirst(fmt.Sprintf("%s", t)) - } -} - var ErrPromptTerminated = errdefs.Cancelled(errors.New("prompt terminated")) // DisableInputEcho disables input echo on the provided streams.In.