1
0
mirror of https://github.com/docker/cli.git synced 2026-11-01 23:35:23 +03:00

Merge pull request #6874 from thaJeztah/future_proof_prefix

formatting: only strip "/" prefixes
This commit is contained in:
Paweł Gronowski
2026-04-02 11:42:31 +02:00
committed by GitHub
2 changed files with 7 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ package container
import (
"strconv"
"strings"
"sync"
"github.com/docker/cli/cli/command/formatter"
@@ -167,9 +168,9 @@ func (c *statsContext) Container() string {
}
func (c *statsContext) Name() string {
// TODO(thaJeztah): make this explicitly trim the "/" prefix, not just any char.
if len(c.s.Name) > 1 {
return c.s.Name[1:]
// Trim the "/" prefix (if present).
if name := strings.TrimPrefix(c.s.Name, "/"); name != "" {
return name
}
return noValue
}

View File

@@ -155,11 +155,12 @@ func (c *ContainerContext) Names() string {
return strings.Join(names, ",")
}
// StripNamePrefix removes prefix from string, typically container names as returned by `ContainersList` API
// StripNamePrefix removes any "/" prefix from container names returned
// by the "ContainersList" API.
func StripNamePrefix(ss []string) []string {
sss := make([]string, len(ss))
for i, s := range ss {
sss[i] = s[1:]
sss[i] = strings.TrimPrefix(s, "/")
}
return sss
}