sort tags for docker also

This commit is contained in:
Quentin 2023-05-03 12:14:06 +02:00
parent a9e4924228
commit 559506c170
Signed by: quentin
GPG Key ID: E9602264D639FF68
1 changed files with 36 additions and 10 deletions

View File

@ -10,8 +10,10 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/containers/image/v5/copy"
"github.com/containers/image/v5/signature"
@ -466,6 +468,12 @@ func (o *OCIMultiArch) UploadImageS3(buck *blob.Bucket) error {
type StaticRegistryManager struct {
name string
buck *blob.Bucket
images []ImageDescriptor
}
type ImageDescriptor struct {
Tag string
Date time.Time
}
func NewStaticRegistryManager(buck *blob.Bucket, name string) *StaticRegistryManager {
@ -480,14 +488,10 @@ type TagList struct {
Tags []string `json:"tags"`
}
func (l *StaticRegistryManager) ComputeTagList() (TagList, error) {
func (l *StaticRegistryManager) Scan() error {
digestPrefix := "sha256:"
cutDigestPrefix := len(digestPrefix)
tagList := TagList{
Name: l.name,
}
iter := l.buck.List(&blob.ListOptions{
Prefix: fmt.Sprintf("v2/%s/manifests/", l.name),
Delimiter: "/",
@ -498,12 +502,12 @@ func (l *StaticRegistryManager) ComputeTagList() (TagList, error) {
break
}
if err != nil {
return tagList, err
return err
}
ksplit := strings.Split(obj.Key, "/")
if len(ksplit) < 1 {
return tagList, errors.New(fmt.Sprintf("Invalid key name %s", obj.Key))
return errors.New(fmt.Sprintf("Invalid key name %s", obj.Key))
}
fname := ksplit[len(ksplit)-1]
@ -511,18 +515,40 @@ func (l *StaticRegistryManager) ComputeTagList() (TagList, error) {
// we ignore sha256 addressed manifests
continue
}
tagList.Tags = append(tagList.Tags, fname)
id := ImageDescriptor {
Tag: fname,
Date: obj.ModTime,
}
l.images = append(l.images, id)
}
return tagList, nil
return nil
}
func (l *StaticRegistryManager) TagList() TagList {
// Sort by date desc
sort.Slice(l.images, func(i, j int) bool {
return l.images[i].Date.After(l.images[j].Date)
})
// Build tagList
tagList := TagList {
Name: l.name,
}
for _, img := range l.images {
tagList.Tags = append(tagList.Tags, img.Tag)
}
return tagList
}
func (l *StaticRegistryManager) UpdateTagList() error {
fmt.Printf("--- update taglist ---\n")
tagList, err := l.ComputeTagList()
err := l.Scan()
if err != nil {
return err
}
tagList := l.TagList()
fmt.Printf("computed tag list: %v\n", tagList)
txt, err := json.Marshal(tagList)