Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

name: Add tag.WithDigest. #1959

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading