Skip to content

Commit

Permalink
name: Add tag.WithDigest.
Browse files Browse the repository at this point in the history
Adds a new function to tags to allow appending a digest to the string
output. This allows users to output strings of the form
`repo:tag@digest`, which can often be used to display concise tag +
digest strings.

NOTE: Most clients parsing this string will ignore the tag in favor of the
digest (though there are a few exceptions like dependabot that will
place meaning in the tag). This is still useful for displaying resolved
tags to humans.
  • Loading branch information
wlynch committed Jun 12, 2024
1 parent 1b4e407 commit aaa71e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/name/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ func (t Tag) Name() string {
return t.Repository.Name() + tagDelim + t.TagStr()
}

// WithDigest returns the tag string with the given digest appended.
// This allows for string creation like "my/repo:tag@sha256:abc123".
// **Note**: this does not validate the digest matches the tag.
func (t Tag) WithDigest(digest string) string {
return strings.Join([]string{t.Repository.Name(), tagDelim, t.TagStr(), digestDelim, digest}, "")
}

// String returns the original input string.
func (t Tag) String() string {
return t.original
Expand Down
14 changes: 14 additions & 0 deletions pkg/name/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,17 @@ func TestOverrideDefault(t *testing.T) {
t.Errorf("Name() was incorrect for %v. Wanted: `%s` Got: `%s`", tag, expectedName, actualName)
}
}

func TestWithTag(t *testing.T) {
tagNameStr := "ubuntu"
tag, err := NewTag(tagNameStr)
if err != nil {
t.Fatalf("`%s` should be a valid Tag name, got error: %v", tagNameStr, err)
}

expectedName := "index.docker.io/library/ubuntu:latest@sha256:deadbeef"
actualName := tag.WithDigest("sha256:deadbeef")
if actualName != expectedName {
t.Errorf("Name() was incorrect for %v. Wanted: `%s` Got: `%s`", tag, expectedName, actualName)
}
}

0 comments on commit aaa71e4

Please sign in to comment.