-
Notifications
You must be signed in to change notification settings - Fork 61
/
tag.go
69 lines (56 loc) · 1.54 KB
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package v1
import (
"carvel.dev/imgpkg/pkg/imgpkg/registry"
regname "github.com/google/go-containerregistry/pkg/name"
)
// TagInfo Contains the tag name and the digest associated with the tag
// TagInfo.Digest might be empty if caller ask for it not to be retrieved
type TagInfo struct {
Tag string
Digest string
}
// TagsInfo Contains all the tags associated with the repository on Image
type TagsInfo struct {
Repository string
Tags []TagInfo
}
// TagList Retrieve all the tags associated with a repository
// imageRef contains the address for the repository
// getDigests when set to true, provides
func TagList(imageRef string, getDigests bool, registryOpts registry.Opts) (TagsInfo, error) {
reg, err := registry.NewSimpleRegistry(registryOpts)
if err != nil {
return TagsInfo{}, err
}
ref, err := regname.ParseReference(imageRef, regname.WeakValidation)
if err != nil {
return TagsInfo{}, err
}
tags, err := reg.ListTags(ref.Context())
if err != nil {
return TagsInfo{}, err
}
tagList := TagsInfo{
Repository: ref.Context().String(),
}
for _, tag := range tags {
tagInfo := TagInfo{
Tag: tag,
}
if getDigests {
tagRef, err := regname.NewTag(ref.Context().String()+":"+tag, regname.WeakValidation)
if err != nil {
return TagsInfo{}, err
}
hash, err := reg.Digest(tagRef)
if err != nil {
return TagsInfo{}, err
}
tagInfo.Digest = hash.String()
}
tagList.Tags = append(tagList.Tags, tagInfo)
}
return tagList, nil
}