Skip to content

Commit ce3090c

Browse files
committed
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 <github@gone.nl>
1 parent 2eec746 commit ce3090c

6 files changed

Lines changed: 40 additions & 42 deletions

File tree

cli/command/config/formatter.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66
"time"
77

8-
"github.com/docker/cli/cli/command"
98
"github.com/docker/cli/cli/command/formatter"
109
"github.com/docker/cli/cli/command/inspect"
1110
"github.com/docker/docker/api/types/swarm"
@@ -157,11 +156,11 @@ func (ctx *configInspectContext) Labels() map[string]string {
157156
}
158157

159158
func (ctx *configInspectContext) CreatedAt() string {
160-
return command.PrettyPrint(ctx.Config.CreatedAt)
159+
return formatter.PrettyPrint(ctx.Config.CreatedAt)
161160
}
162161

163162
func (ctx *configInspectContext) UpdatedAt() string {
164-
return command.PrettyPrint(ctx.Config.UpdatedAt)
163+
return formatter.PrettyPrint(ctx.Config.UpdatedAt)
165164
}
166165

167166
func (ctx *configInspectContext) Data() string {

cli/command/formatter/displayutils.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package formatter
22

33
import (
4+
"fmt"
5+
"strings"
46
"unicode/utf8"
57

68
"golang.org/x/text/width"
@@ -59,3 +61,27 @@ func Ellipsis(s string, maxDisplayWidth int) string {
5961
}
6062
return s
6163
}
64+
65+
// capitalizeFirst capitalizes the first character of string
66+
func capitalizeFirst(s string) string {
67+
switch l := len(s); l {
68+
case 0:
69+
return s
70+
case 1:
71+
return strings.ToLower(s)
72+
default:
73+
return strings.ToUpper(string(s[0])) + strings.ToLower(s[1:])
74+
}
75+
}
76+
77+
// PrettyPrint outputs arbitrary data for human formatted output by uppercasing the first letter.
78+
func PrettyPrint(i any) string {
79+
switch t := i.(type) {
80+
case nil:
81+
return "None"
82+
case string:
83+
return capitalizeFirst(t)
84+
default:
85+
return capitalizeFirst(fmt.Sprintf("%s", t))
86+
}
87+
}

cli/command/node/formatter.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"reflect"
77
"strings"
88

9-
"github.com/docker/cli/cli/command"
109
"github.com/docker/cli/cli/command/formatter"
1110
"github.com/docker/cli/cli/command/inspect"
1211
"github.com/docker/docker/api/types/swarm"
@@ -147,11 +146,11 @@ func (c *nodeContext) Hostname() string {
147146
}
148147

149148
func (c *nodeContext) Status() string {
150-
return command.PrettyPrint(string(c.n.Status.State))
149+
return formatter.PrettyPrint(string(c.n.Status.State))
151150
}
152151

153152
func (c *nodeContext) Availability() string {
154-
return command.PrettyPrint(string(c.n.Spec.Availability))
153+
return formatter.PrettyPrint(string(c.n.Spec.Availability))
155154
}
156155

157156
func (c *nodeContext) ManagerStatus() string {
@@ -163,7 +162,7 @@ func (c *nodeContext) ManagerStatus() string {
163162
reachability = string(c.n.ManagerStatus.Reachability)
164163
}
165164
}
166-
return command.PrettyPrint(reachability)
165+
return formatter.PrettyPrint(reachability)
167166
}
168167

169168
func (c *nodeContext) TLSStatus() string {
@@ -226,23 +225,23 @@ func (ctx *nodeInspectContext) Hostname() string {
226225
}
227226

228227
func (ctx *nodeInspectContext) CreatedAt() string {
229-
return command.PrettyPrint(ctx.Node.CreatedAt)
228+
return formatter.PrettyPrint(ctx.Node.CreatedAt)
230229
}
231230

232231
func (ctx *nodeInspectContext) StatusState() string {
233-
return command.PrettyPrint(ctx.Node.Status.State)
232+
return formatter.PrettyPrint(ctx.Node.Status.State)
234233
}
235234

236235
func (ctx *nodeInspectContext) HasStatusMessage() bool {
237236
return ctx.Node.Status.Message != ""
238237
}
239238

240239
func (ctx *nodeInspectContext) StatusMessage() string {
241-
return command.PrettyPrint(ctx.Node.Status.Message)
240+
return formatter.PrettyPrint(ctx.Node.Status.Message)
242241
}
243242

244243
func (ctx *nodeInspectContext) SpecAvailability() string {
245-
return command.PrettyPrint(ctx.Node.Spec.Availability)
244+
return formatter.PrettyPrint(ctx.Node.Spec.Availability)
246245
}
247246

248247
func (ctx *nodeInspectContext) HasStatusAddr() bool {
@@ -262,7 +261,7 @@ func (ctx *nodeInspectContext) ManagerStatusAddr() string {
262261
}
263262

264263
func (ctx *nodeInspectContext) ManagerStatusReachability() string {
265-
return command.PrettyPrint(ctx.Node.ManagerStatus.Reachability)
264+
return formatter.PrettyPrint(ctx.Node.ManagerStatus.Reachability)
266265
}
267266

268267
func (ctx *nodeInspectContext) IsManagerStatusLeader() bool {

cli/command/secret/formatter.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66
"time"
77

8-
"github.com/docker/cli/cli/command"
98
"github.com/docker/cli/cli/command/formatter"
109
"github.com/docker/cli/cli/command/inspect"
1110
"github.com/docker/docker/api/types/swarm"
@@ -171,9 +170,9 @@ func (ctx *secretInspectContext) Driver() string {
171170
}
172171

173172
func (ctx *secretInspectContext) CreatedAt() string {
174-
return command.PrettyPrint(ctx.Secret.CreatedAt)
173+
return formatter.PrettyPrint(ctx.Secret.CreatedAt)
175174
}
176175

177176
func (ctx *secretInspectContext) UpdatedAt() string {
178-
return command.PrettyPrint(ctx.Secret.UpdatedAt)
177+
return formatter.PrettyPrint(ctx.Secret.UpdatedAt)
179178
}

cli/command/task/formatter.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/distribution/reference"
9-
"github.com/docker/cli/cli/command"
109
"github.com/docker/cli/cli/command/formatter"
1110
"github.com/docker/docker/api/types/swarm"
1211
"github.com/docker/docker/pkg/stringid"
@@ -110,12 +109,12 @@ func (c *taskContext) Node() string {
110109
}
111110

112111
func (c *taskContext) DesiredState() string {
113-
return command.PrettyPrint(c.task.DesiredState)
112+
return formatter.PrettyPrint(c.task.DesiredState)
114113
}
115114

116115
func (c *taskContext) CurrentState() string {
117116
return fmt.Sprintf("%s %s ago",
118-
command.PrettyPrint(c.task.Status.State),
117+
formatter.PrettyPrint(c.task.Status.State),
119118
strings.ToLower(units.HumanDuration(time.Since(c.task.Status.Timestamp))),
120119
)
121120
}

cli/command/utils.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,6 @@ func CopyToFile(outfile string, r io.Reader) error {
5151
return nil
5252
}
5353

54-
// capitalizeFirst capitalizes the first character of string
55-
func capitalizeFirst(s string) string {
56-
switch l := len(s); l {
57-
case 0:
58-
return s
59-
case 1:
60-
return strings.ToLower(s)
61-
default:
62-
return strings.ToUpper(string(s[0])) + strings.ToLower(s[1:])
63-
}
64-
}
65-
66-
// PrettyPrint outputs arbitrary data for human formatted output by uppercasing the first letter.
67-
func PrettyPrint(i any) string {
68-
switch t := i.(type) {
69-
case nil:
70-
return "None"
71-
case string:
72-
return capitalizeFirst(t)
73-
default:
74-
return capitalizeFirst(fmt.Sprintf("%s", t))
75-
}
76-
}
77-
7854
var ErrPromptTerminated = errdefs.Cancelled(errors.New("prompt terminated"))
7955

8056
// DisableInputEcho disables input echo on the provided streams.In.

0 commit comments

Comments
 (0)