feat(log): use short image/container IDs in logs (#888)

This commit is contained in:
nils måsén 2021-04-18 18:34:38 +02:00 committed by GitHub
parent 62a6d31880
commit 6a9d985ce7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 11 deletions

23
pkg/container/util.go Normal file
View file

@ -0,0 +1,23 @@
package container
import "strings"
// ShortID returns the 12-character (hex) short version of an image ID hash, removing any "sha256:" prefix if present
func ShortID(imageID string) (short string) {
prefixSep := strings.IndexRune(imageID, ':')
offset := 0
length := 12
if prefixSep >= 0 {
if imageID[0:prefixSep] == "sha256" {
offset = prefixSep + 1
} else {
length += prefixSep + 1
}
}
if len(imageID) >= offset+length {
return imageID[offset : offset+length]
}
return imageID
}