Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 450a8e4

Browse filesBrowse files
committed
pkg/stringid: move to daemon, and provide copy in client
The stringid package is used in many places; while it's trivial to implement a similar utility, let's just provide it as a utility package in the client, removing the daemon-specific logic. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 3205fcf commit 450a8e4
Copy full SHA for 450a8e4

65 files changed

+227-58Lines changed: 227 additions & 58 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎client/pkg/stringid/stringid.go‎

Copy file name to clipboard
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Package stringid provides helper functions for dealing with string identifiers.
2+
//
3+
// It is similar to the package used by the daemon, but for presentational
4+
// purposes in the client.
5+
package stringid
6+
7+
import (
8+
"crypto/rand"
9+
"encoding/hex"
10+
"strings"
11+
)
12+
13+
const (
14+
shortLen = 12
15+
fullLen = 64
16+
)
17+
18+
// TruncateID returns a shorthand version of a string identifier for presentation.
19+
// For convenience, it accepts both digests ("sha256:xxxx") and IDs without an
20+
// algorithm prefix. It truncates the algorithm (if any) before truncating the
21+
// ID. The length of the truncated ID is currently fixed, but users should make
22+
// no assumptions of this to not change; it is merely a prefix of the ID that
23+
// provides enough uniqueness for common scenarios.
24+
//
25+
// Truncated IDs ("ID-prefixes") usually can be used to uniquely identify an
26+
// object (such as a container or network), but collisions may happen, in
27+
// which case an "ambiguous result" error is produced. In case of a collision,
28+
// the caller should try with a longer prefix or the full-length ID.
29+
func TruncateID(id string) string {
30+
if i := strings.IndexRune(id, ':'); i >= 0 {
31+
id = id[i+1:]
32+
}
33+
if len(id) > shortLen {
34+
id = id[:shortLen]
35+
}
36+
return id
37+
}
38+
39+
// GenerateRandomID returns a unique, 64-character ID consisting of a-z, 0-9.
40+
func GenerateRandomID() string {
41+
b := make([]byte, 32)
42+
if _, err := rand.Read(b); err != nil {
43+
panic(err) // This shouldn't happen
44+
}
45+
id := hex.EncodeToString(b)
46+
return id
47+
}
Collapse file
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package stringid
2+
3+
import "testing"
4+
5+
func TestGenerateRandomID(t *testing.T) {
6+
id := GenerateRandomID()
7+
8+
if len(id) != fullLen {
9+
t.Fatalf("Id returned is incorrect: %s", id)
10+
}
11+
}
12+
13+
func TestTruncateID(t *testing.T) {
14+
tests := []struct {
15+
doc, id, expected string
16+
}{
17+
{
18+
doc: "empty ID",
19+
id: "",
20+
expected: "",
21+
},
22+
{
23+
// IDs are expected to be 12 (short) or 64 characters, and not be numeric only,
24+
// but TruncateID should handle these gracefully.
25+
doc: "invalid ID",
26+
id: "1234",
27+
expected: "1234",
28+
},
29+
{
30+
doc: "full ID",
31+
id: "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2",
32+
expected: "90435eec5c4e",
33+
},
34+
{
35+
doc: "digest",
36+
id: "sha256:90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2",
37+
expected: "90435eec5c4e",
38+
},
39+
{
40+
doc: "very long ID",
41+
id: "90435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a290435eec5c4e124e741ef731e118be2fc799a68aba0466ec17717f24ce2ae6a2",
42+
expected: "90435eec5c4e",
43+
},
44+
}
45+
46+
for _, tc := range tests {
47+
t.Run(tc.doc, func(t *testing.T) {
48+
actual := TruncateID(tc.id)
49+
if actual != tc.expected {
50+
t.Errorf("expected: %q, got: %q", tc.expected, actual)
51+
}
52+
})
53+
}
54+
}
Collapse file

‎daemon/builder/backend/backend.go‎

Copy file name to clipboardExpand all lines: daemon/builder/backend/backend.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/docker/docker/daemon/builder"
1010
daemonevents "github.com/docker/docker/daemon/events"
1111
buildkit "github.com/docker/docker/daemon/internal/builder-next"
12+
"github.com/docker/docker/daemon/pkg/stringid"
1213
"github.com/docker/docker/image"
13-
"github.com/docker/docker/pkg/stringid"
1414
"github.com/moby/moby/api/types/backend"
1515
"github.com/moby/moby/api/types/build"
1616
"github.com/moby/moby/api/types/events"
Collapse file

‎daemon/builder/dockerfile/builder.go‎

Copy file name to clipboardExpand all lines: daemon/builder/dockerfile/builder.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/containerd/platforms"
1313
"github.com/docker/docker/daemon/builder"
1414
"github.com/docker/docker/daemon/builder/remotecontext"
15+
"github.com/docker/docker/daemon/pkg/stringid"
1516
"github.com/docker/docker/errdefs"
16-
"github.com/docker/docker/pkg/stringid"
1717
"github.com/moby/buildkit/frontend/dockerfile/instructions"
1818
"github.com/moby/buildkit/frontend/dockerfile/parser"
1919
"github.com/moby/buildkit/frontend/dockerfile/shell"
Collapse file

‎daemon/builder/dockerfile/containerbackend.go‎

Copy file name to clipboardExpand all lines: daemon/builder/dockerfile/containerbackend.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
cerrdefs "github.com/containerd/errdefs"
99
"github.com/containerd/log"
1010
"github.com/docker/docker/daemon/builder"
11-
"github.com/docker/docker/pkg/stringid"
11+
"github.com/docker/docker/daemon/pkg/stringid"
1212
"github.com/moby/moby/api/types/backend"
1313
"github.com/moby/moby/api/types/container"
1414
"github.com/pkg/errors"
Collapse file

‎daemon/builder/dockerfile/internals.go‎

Copy file name to clipboardExpand all lines: daemon/builder/dockerfile/internals.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
"github.com/containerd/platforms"
1515
"github.com/docker/docker/daemon/builder"
1616
networkSettings "github.com/docker/docker/daemon/network"
17+
"github.com/docker/docker/daemon/pkg/stringid"
1718
"github.com/docker/docker/image"
18-
"github.com/docker/docker/pkg/stringid"
1919
"github.com/docker/go-connections/nat"
2020
"github.com/moby/go-archive"
2121
"github.com/moby/go-archive/chrootarchive"
Collapse file

‎daemon/cluster/executor/container/validate_test.go‎

Copy file name to clipboardExpand all lines: daemon/cluster/executor/container/validate_test.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77

88
"github.com/docker/docker/daemon"
9-
"github.com/docker/docker/pkg/stringid"
9+
"github.com/docker/docker/daemon/pkg/stringid"
1010
"github.com/moby/swarmkit/v2/api"
1111
)
1212

Collapse file

‎daemon/container/exec.go‎

Copy file name to clipboardExpand all lines: daemon/container/exec.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/containerd/log"
1010
"github.com/docker/docker/daemon/internal/libcontainerd/types"
1111
"github.com/docker/docker/daemon/internal/stream"
12-
"github.com/docker/docker/pkg/stringid"
12+
"github.com/docker/docker/daemon/pkg/stringid"
1313
)
1414

1515
// ExecConfig holds the configurations for execs. The Daemon keeps
Collapse file

‎daemon/container/view_test.go‎

Copy file name to clipboardExpand all lines: daemon/container/view_test.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"testing"
99

1010
cerrdefs "github.com/containerd/errdefs"
11-
"github.com/docker/docker/pkg/stringid"
11+
"github.com/docker/docker/daemon/pkg/stringid"
1212
"github.com/google/uuid"
1313
"github.com/moby/moby/api/types/container"
1414
"gotest.tools/v3/assert"
Collapse file

‎daemon/container_operations.go‎

Copy file name to clipboardExpand all lines: daemon/container_operations.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
"github.com/docker/docker/daemon/libnetwork/types"
2525
"github.com/docker/docker/daemon/network"
2626
"github.com/docker/docker/daemon/pkg/opts"
27+
"github.com/docker/docker/daemon/pkg/stringid"
2728
"github.com/docker/docker/errdefs"
2829
"github.com/docker/docker/internal/multierror"
2930
"github.com/docker/docker/internal/sliceutil"
30-
"github.com/docker/docker/pkg/stringid"
3131
"github.com/docker/docker/runconfig"
3232
"github.com/docker/go-connections/nat"
3333
containertypes "github.com/moby/moby/api/types/container"

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.