Skip to content

Commit dc9b6e5

Browse files
authored
Merge pull request #6874 from thaJeztah/future_proof_prefix
formatting: only strip "/" prefixes
2 parents f4d2906 + dfda342 commit dc9b6e5

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

cli/command/container/formatter_stats.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package container
22

33
import (
44
"strconv"
5+
"strings"
56
"sync"
67

78
"github.com/docker/cli/cli/command/formatter"
@@ -167,9 +168,9 @@ func (c *statsContext) Container() string {
167168
}
168169

169170
func (c *statsContext) Name() string {
170-
// TODO(thaJeztah): make this explicitly trim the "/" prefix, not just any char.
171-
if len(c.s.Name) > 1 {
172-
return c.s.Name[1:]
171+
// Trim the "/" prefix (if present).
172+
if name := strings.TrimPrefix(c.s.Name, "/"); name != "" {
173+
return name
173174
}
174175
return noValue
175176
}

cli/command/formatter/container.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,12 @@ func (c *ContainerContext) Names() string {
155155
return strings.Join(names, ",")
156156
}
157157

158-
// StripNamePrefix removes prefix from string, typically container names as returned by `ContainersList` API
158+
// StripNamePrefix removes any "/" prefix from container names returned
159+
// by the "ContainersList" API.
159160
func StripNamePrefix(ss []string) []string {
160161
sss := make([]string, len(ss))
161162
for i, s := range ss {
162-
sss[i] = s[1:]
163+
sss[i] = strings.TrimPrefix(s, "/")
163164
}
164165
return sss
165166
}

0 commit comments

Comments
 (0)