Merge pull request #5916 from thaJeztah/move_command_prettyprint
cli/command: move PrettyPrint utility to cli/command/formatter
This commit is contained in:
commit
bc57a035c4
@ -5,7 +5,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/command"
|
|
||||||
"github.com/docker/cli/cli/command/formatter"
|
"github.com/docker/cli/cli/command/formatter"
|
||||||
"github.com/docker/cli/cli/command/inspect"
|
"github.com/docker/cli/cli/command/inspect"
|
||||||
"github.com/docker/docker/api/types/swarm"
|
"github.com/docker/docker/api/types/swarm"
|
||||||
@ -157,11 +156,11 @@ func (ctx *configInspectContext) Labels() map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *configInspectContext) CreatedAt() string {
|
func (ctx *configInspectContext) CreatedAt() string {
|
||||||
return command.PrettyPrint(ctx.Config.CreatedAt)
|
return formatter.PrettyPrint(ctx.Config.CreatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *configInspectContext) UpdatedAt() string {
|
func (ctx *configInspectContext) UpdatedAt() string {
|
||||||
return command.PrettyPrint(ctx.Config.UpdatedAt)
|
return formatter.PrettyPrint(ctx.Config.UpdatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *configInspectContext) Data() string {
|
func (ctx *configInspectContext) Data() string {
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package formatter
|
package formatter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"golang.org/x/text/width"
|
"golang.org/x/text/width"
|
||||||
@ -59,3 +61,27 @@ func Ellipsis(s string, maxDisplayWidth int) string {
|
|||||||
}
|
}
|
||||||
return s
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/command"
|
|
||||||
"github.com/docker/cli/cli/command/formatter"
|
"github.com/docker/cli/cli/command/formatter"
|
||||||
"github.com/docker/cli/cli/command/inspect"
|
"github.com/docker/cli/cli/command/inspect"
|
||||||
"github.com/docker/docker/api/types/swarm"
|
"github.com/docker/docker/api/types/swarm"
|
||||||
@ -147,11 +146,11 @@ func (c *nodeContext) Hostname() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *nodeContext) Status() 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 {
|
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 {
|
func (c *nodeContext) ManagerStatus() string {
|
||||||
@ -163,7 +162,7 @@ func (c *nodeContext) ManagerStatus() string {
|
|||||||
reachability = string(c.n.ManagerStatus.Reachability)
|
reachability = string(c.n.ManagerStatus.Reachability)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return command.PrettyPrint(reachability)
|
return formatter.PrettyPrint(reachability)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *nodeContext) TLSStatus() string {
|
func (c *nodeContext) TLSStatus() string {
|
||||||
@ -226,11 +225,11 @@ func (ctx *nodeInspectContext) Hostname() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *nodeInspectContext) CreatedAt() string {
|
func (ctx *nodeInspectContext) CreatedAt() string {
|
||||||
return command.PrettyPrint(ctx.Node.CreatedAt)
|
return formatter.PrettyPrint(ctx.Node.CreatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *nodeInspectContext) StatusState() string {
|
func (ctx *nodeInspectContext) StatusState() string {
|
||||||
return command.PrettyPrint(ctx.Node.Status.State)
|
return formatter.PrettyPrint(ctx.Node.Status.State)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *nodeInspectContext) HasStatusMessage() bool {
|
func (ctx *nodeInspectContext) HasStatusMessage() bool {
|
||||||
@ -238,11 +237,11 @@ func (ctx *nodeInspectContext) HasStatusMessage() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *nodeInspectContext) StatusMessage() string {
|
func (ctx *nodeInspectContext) StatusMessage() string {
|
||||||
return command.PrettyPrint(ctx.Node.Status.Message)
|
return formatter.PrettyPrint(ctx.Node.Status.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *nodeInspectContext) SpecAvailability() string {
|
func (ctx *nodeInspectContext) SpecAvailability() string {
|
||||||
return command.PrettyPrint(ctx.Node.Spec.Availability)
|
return formatter.PrettyPrint(ctx.Node.Spec.Availability)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *nodeInspectContext) HasStatusAddr() bool {
|
func (ctx *nodeInspectContext) HasStatusAddr() bool {
|
||||||
@ -262,7 +261,7 @@ func (ctx *nodeInspectContext) ManagerStatusAddr() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *nodeInspectContext) ManagerStatusReachability() 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 {
|
func (ctx *nodeInspectContext) IsManagerStatusLeader() bool {
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/command"
|
|
||||||
"github.com/docker/cli/cli/command/formatter"
|
"github.com/docker/cli/cli/command/formatter"
|
||||||
"github.com/docker/cli/cli/command/inspect"
|
"github.com/docker/cli/cli/command/inspect"
|
||||||
"github.com/docker/docker/api/types/swarm"
|
"github.com/docker/docker/api/types/swarm"
|
||||||
@ -171,9 +170,9 @@ func (ctx *secretInspectContext) Driver() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *secretInspectContext) CreatedAt() string {
|
func (ctx *secretInspectContext) CreatedAt() string {
|
||||||
return command.PrettyPrint(ctx.Secret.CreatedAt)
|
return formatter.PrettyPrint(ctx.Secret.CreatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *secretInspectContext) UpdatedAt() string {
|
func (ctx *secretInspectContext) UpdatedAt() string {
|
||||||
return command.PrettyPrint(ctx.Secret.UpdatedAt)
|
return formatter.PrettyPrint(ctx.Secret.UpdatedAt)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/distribution/reference"
|
"github.com/distribution/reference"
|
||||||
"github.com/docker/cli/cli/command"
|
|
||||||
"github.com/docker/cli/cli/command/formatter"
|
"github.com/docker/cli/cli/command/formatter"
|
||||||
"github.com/docker/docker/api/types/swarm"
|
"github.com/docker/docker/api/types/swarm"
|
||||||
"github.com/docker/docker/pkg/stringid"
|
"github.com/docker/docker/pkg/stringid"
|
||||||
@ -110,12 +109,12 @@ func (c *taskContext) Node() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *taskContext) DesiredState() string {
|
func (c *taskContext) DesiredState() string {
|
||||||
return command.PrettyPrint(c.task.DesiredState)
|
return formatter.PrettyPrint(c.task.DesiredState)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *taskContext) CurrentState() string {
|
func (c *taskContext) CurrentState() string {
|
||||||
return fmt.Sprintf("%s %s ago",
|
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))),
|
strings.ToLower(units.HumanDuration(time.Since(c.task.Status.Timestamp))),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -52,30 +52,6 @@ func CopyToFile(outfile string, r io.Reader) error {
|
|||||||
return nil
|
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"))
|
var ErrPromptTerminated = errdefs.Cancelled(errors.New("prompt terminated"))
|
||||||
|
|
||||||
// DisableInputEcho disables input echo on the provided streams.In.
|
// DisableInputEcho disables input echo on the provided streams.In.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user