cli/command/formatter: use alias/wrapper for TruncateID
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/moby/moby/client/pkg/stringid"
|
||||
"golang.org/x/text/width"
|
||||
)
|
||||
|
||||
@@ -27,23 +28,12 @@ func charWidth(r rune) int {
|
||||
}
|
||||
}
|
||||
|
||||
const shortLen = 12
|
||||
|
||||
// TruncateID returns a shorthand version of a string identifier for presentation,
|
||||
// after trimming digest algorithm prefix (if any).
|
||||
//
|
||||
// This function is a copy of [stringid.TruncateID] for presentation / formatting
|
||||
// purposes.
|
||||
//
|
||||
// [stringid.TruncateID]: https://github.com/moby/moby/blob/v28.3.2/pkg/stringid/stringid.go#L19
|
||||
// This function is a wrapper for [stringid.TruncateID] for convenience.
|
||||
func TruncateID(id string) string {
|
||||
if i := strings.IndexRune(id, ':'); i >= 0 {
|
||||
id = id[i+1:]
|
||||
}
|
||||
if len(id) > shortLen {
|
||||
id = id[:shortLen]
|
||||
}
|
||||
return id
|
||||
return stringid.TruncateID(id)
|
||||
}
|
||||
|
||||
// Ellipsis truncates a string to fit within maxDisplayWidth, and appends ellipsis (…).
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
// Package stringid provides helper functions for dealing with string identifiers.
|
||||
//
|
||||
// It is similar to the package used by the daemon, but for presentational
|
||||
// purposes in the client.
|
||||
package stringid
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
shortLen = 12
|
||||
fullLen = 64
|
||||
)
|
||||
|
||||
// TruncateID returns a shorthand version of a string identifier for presentation.
|
||||
// For convenience, it accepts both digests ("sha256:xxxx") and IDs without an
|
||||
// algorithm prefix. It truncates the algorithm (if any) before truncating the
|
||||
// ID. The length of the truncated ID is currently fixed, but users should make
|
||||
// no assumptions of this to not change; it is merely a prefix of the ID that
|
||||
// provides enough uniqueness for common scenarios.
|
||||
//
|
||||
// Truncated IDs ("ID-prefixes") usually can be used to uniquely identify an
|
||||
// object (such as a container or network), but collisions may happen, in
|
||||
// which case an "ambiguous result" error is produced. In case of a collision,
|
||||
// the caller should try with a longer prefix or the full-length ID.
|
||||
func TruncateID(id string) string {
|
||||
if i := strings.IndexRune(id, ':'); i >= 0 {
|
||||
id = id[i+1:]
|
||||
}
|
||||
if len(id) > shortLen {
|
||||
id = id[:shortLen]
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// GenerateRandomID returns a unique, 64-character ID consisting of a-z, 0-9.
|
||||
func GenerateRandomID() string {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err) // This shouldn't happen
|
||||
}
|
||||
id := hex.EncodeToString(b)
|
||||
return id
|
||||
}
|
||||
Vendored
+1
@@ -201,6 +201,7 @@ github.com/moby/moby/api/types/volume
|
||||
# github.com/moby/moby/client v0.0.0 => github.com/moby/moby/client v0.0.0-20250726000215-c4afa7715715
|
||||
## explicit; go 1.23.0
|
||||
github.com/moby/moby/client
|
||||
github.com/moby/moby/client/pkg/stringid
|
||||
# github.com/moby/patternmatcher v0.6.0
|
||||
## explicit; go 1.19
|
||||
github.com/moby/patternmatcher
|
||||
|
||||
Reference in New Issue
Block a user