Skip to content

Commit 7d574b8

Browse files
authored
Merge pull request #6180 from thaJeztah/truncate_id
remove uses of github.com/docker/docker/pkg/stringid
2 parents 7668b68 + 9b047a5 commit 7d574b8

File tree

28 files changed

+124
-118
lines changed

28 files changed

+124
-118
lines changed

cli/command/container/formatter_stats.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"sync"
66

77
"github.com/docker/cli/cli/command/formatter"
8-
"github.com/docker/docker/pkg/stringid"
98
units "github.com/docker/go-units"
109
)
1110

@@ -176,7 +175,7 @@ func (c *statsContext) Name() string {
176175

177176
func (c *statsContext) ID() string {
178177
if c.trunc {
179-
return stringid.TruncateID(c.s.ID)
178+
return formatter.TruncateID(c.s.ID)
180179
}
181180
return c.s.ID
182181
}

cli/command/container/formatter_stats_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"testing"
66

77
"github.com/docker/cli/cli/command/formatter"
8-
"github.com/docker/docker/pkg/stringid"
8+
"github.com/docker/cli/internal/test"
99
"gotest.tools/v3/assert"
1010
is "gotest.tools/v3/assert/cmp"
1111
)
1212

1313
func TestContainerStatsContext(t *testing.T) {
14-
containerID := stringid.GenerateRandomID()
14+
containerID := test.RandomID()
1515

1616
var ctx statsContext
1717
tt := []struct {

cli/command/formatter/buildcache.go

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

99
"github.com/docker/docker/api/types/build"
10-
"github.com/docker/docker/pkg/stringid"
1110
"github.com/docker/go-units"
1211
)
1312

@@ -115,7 +114,7 @@ func (c *buildCacheContext) MarshalJSON() ([]byte, error) {
115114
func (c *buildCacheContext) ID() string {
116115
id := c.v.ID
117116
if c.trunc {
118-
id = stringid.TruncateID(c.v.ID)
117+
id = TruncateID(c.v.ID)
119118
}
120119
if c.v.InUse {
121120
return id + "*"
@@ -131,7 +130,7 @@ func (c *buildCacheContext) Parent() string {
131130
parent = c.v.Parent //nolint:staticcheck // Ignore SA1019: Field was deprecated in API v1.42, but kept for backward compatibility
132131
}
133132
if c.trunc {
134-
return stringid.TruncateID(parent)
133+
return TruncateID(parent)
135134
}
136135
return parent
137136
}

cli/command/formatter/container.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/containerd/platforms"
1515
"github.com/distribution/reference"
1616
"github.com/docker/docker/api/types/container"
17-
"github.com/docker/docker/pkg/stringid"
1817
"github.com/docker/go-units"
1918
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2019
)
@@ -135,7 +134,7 @@ func (c *ContainerContext) MarshalJSON() ([]byte, error) {
135134
// option being set, the full or truncated ID is returned.
136135
func (c *ContainerContext) ID() string {
137136
if c.trunc {
138-
return stringid.TruncateID(c.c.ID)
137+
return TruncateID(c.c.ID)
139138
}
140139
return c.c.ID
141140
}
@@ -172,7 +171,7 @@ func (c *ContainerContext) Image() string {
172171
return "<no image>"
173172
}
174173
if c.trunc {
175-
if trunc := stringid.TruncateID(c.c.ImageID); trunc == stringid.TruncateID(c.c.Image) {
174+
if trunc := TruncateID(c.c.ImageID); trunc == TruncateID(c.c.Image) {
176175
return trunc
177176
}
178177
// truncate digest if no-trunc option was not selected

cli/command/formatter/container_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ import (
1313

1414
"github.com/docker/cli/internal/test"
1515
"github.com/docker/docker/api/types/container"
16-
"github.com/docker/docker/pkg/stringid"
1716
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1817
"gotest.tools/v3/assert"
1918
is "gotest.tools/v3/assert/cmp"
2019
"gotest.tools/v3/golden"
2120
)
2221

2322
func TestContainerPsContext(t *testing.T) {
24-
containerID := stringid.GenerateRandomID()
23+
containerID := test.RandomID()
2524
unix := time.Now().Add(-65 * time.Second).Unix()
2625

2726
var ctx ContainerContext
@@ -34,7 +33,7 @@ func TestContainerPsContext(t *testing.T) {
3433
{
3534
container: container.Summary{ID: containerID},
3635
trunc: true,
37-
expValue: stringid.TruncateID(containerID),
36+
expValue: TruncateID(containerID),
3837
call: ctx.ID,
3938
},
4039
{

cli/command/formatter/displayutils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ func charWidth(r rune) int {
2727
}
2828
}
2929

30+
const shortLen = 12
31+
32+
// TruncateID returns a shorthand version of a string identifier for presentation,
33+
// after trimming digest algorithm prefix (if any).
34+
//
35+
// This function is a copy of [stringid.TruncateID] for presentation / formatting
36+
// purposes.
37+
//
38+
// [stringid.TruncateID]: https://github.com/moby/moby/blob/v28.3.2/pkg/stringid/stringid.go#L19
39+
func TruncateID(id string) string {
40+
if i := strings.IndexRune(id, ':'); i >= 0 {
41+
id = id[i+1:]
42+
}
43+
if len(id) > shortLen {
44+
id = id[:shortLen]
45+
}
46+
return id
47+
}
48+
3049
// Ellipsis truncates a string to fit within maxDisplayWidth, and appends ellipsis (…).
3150
// For maxDisplayWidth of 1 and lower, no ellipsis is appended.
3251
// For maxDisplayWidth of 1, first char of string will return even if its width > 1.

cli/command/formatter/displayutils_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,49 @@ import (
77
is "gotest.tools/v3/assert/cmp"
88
)
99

10+
func TestTruncateID(t *testing.T) {
11+
tests := []struct {
12+
doc, id, expected string
13+
}{
14+
{
15+
doc: "empty ID",
16+
id: "",
17+
expected: "",
18+
},
19+
{
20+
// IDs are expected to be 12 (short) or 64 characters, and not be numeric only,
21+
// but TruncateID should handle these gracefully.
22+
doc: "invalid ID",
23+
id: "1234",
24+
expected: "1234",
25+
},
26+
{
27+
doc: "full ID",
28+
id: "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2",
29+
expected: "90435eec5c4e",
30+
},
31+
{
32+
doc: "digest",
33+
id: "sha256:90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2",
34+
expected: "90435eec5c4e",
35+
},
36+
{
37+
doc: "very long ID",
38+
id: "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a290435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2",
39+
expected: "90435eec5c4e",
40+
},
41+
}
42+
43+
for _, tc := range tests {
44+
t.Run(tc.doc, func(t *testing.T) {
45+
actual := TruncateID(tc.id)
46+
if actual != tc.expected {
47+
t.Errorf("expected: %q, got: %q", tc.expected, actual)
48+
}
49+
})
50+
}
51+
}
52+
1053
func TestEllipsis(t *testing.T) {
1154
testcases := []struct {
1255
source string

cli/command/formatter/image.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/distribution/reference"
88
"github.com/docker/docker/api/types/image"
9-
"github.com/docker/docker/pkg/stringid"
109
units "github.com/docker/go-units"
1110
)
1211

@@ -216,7 +215,7 @@ func (c *imageContext) MarshalJSON() ([]byte, error) {
216215

217216
func (c *imageContext) ID() string {
218217
if c.trunc {
219-
return stringid.TruncateID(c.i.ID)
218+
return TruncateID(c.i.ID)
220219
}
221220
return c.i.ID
222221
}

cli/command/formatter/image_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import (
99

1010
"github.com/docker/cli/internal/test"
1111
"github.com/docker/docker/api/types/image"
12-
"github.com/docker/docker/pkg/stringid"
1312
"gotest.tools/v3/assert"
1413
is "gotest.tools/v3/assert/cmp"
1514
)
1615

1716
func TestImageContext(t *testing.T) {
18-
imageID := stringid.GenerateRandomID()
17+
imageID := test.RandomID()
1918
unix := time.Now().Unix()
2019
zeroTime := int64(-62135596800)
2120

@@ -27,7 +26,7 @@ func TestImageContext(t *testing.T) {
2726
}{
2827
{
2928
imageCtx: imageContext{i: image.Summary{ID: imageID}, trunc: true},
30-
expValue: stringid.TruncateID(imageID),
29+
expValue: TruncateID(imageID),
3130
call: ctx.ID,
3231
},
3332
{

cli/command/formatter/volume_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ import (
1212

1313
"github.com/docker/cli/internal/test"
1414
"github.com/docker/docker/api/types/volume"
15-
"github.com/docker/docker/pkg/stringid"
1615
"gotest.tools/v3/assert"
1716
is "gotest.tools/v3/assert/cmp"
1817
)
1918

2019
func TestVolumeContext(t *testing.T) {
21-
volumeName := stringid.GenerateRandomID()
20+
volumeName := test.RandomID()
2221

2322
var ctx volumeContext
2423
cases := []struct {

0 commit comments

Comments
 (0)