Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cli/command/config/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions cli/command/formatter/displayutils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package formatter

import (
"fmt"
"strings"
"unicode/utf8"

"golang.org/x/text/width"
Expand Down Expand Up @@ -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))
}
}
17 changes: 8 additions & 9 deletions cli/command/node/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -226,23 +225,23 @@ 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 {
return ctx.Node.Status.Message != ""
}

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 {
Expand All @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions cli/command/secret/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
5 changes: 2 additions & 3 deletions cli/command/task/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))),
)
}
Expand Down
24 changes: 0 additions & 24 deletions cli/command/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading