sort static tags by desc time

This commit is contained in:
Quentin 2023-05-03 11:36:53 +02:00
parent 40fd4646ce
commit a9e4924228
Signed by: quentin
GPG key ID: E9602264D639FF68

View file

@ -10,7 +10,9 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
"github.com/spf13/cobra"
"gocloud.dev/blob"
@ -39,11 +41,6 @@ type RegistryPlatform struct {
OS string `json:"os"`
}
type Manifest struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}
//---
//--- Collect data on the filesystem
type Platform struct {
@ -137,13 +134,6 @@ func NewArtifact(nametag, path string) (Artifact, error) {
return ar, nil
}
func (a *Artifact) UpdateManifest() Manifest {
return Manifest{
Name: a.Name,
Tags: []string{a.Tag}, //@FIXME we must fetch the other tags of the repo
}
}
func (a *Artifact) BuildTag() Tag {
t := Tag{Flavors: []Flavor{}}
for _, p := range a.Platforms {
@ -186,20 +176,100 @@ func (a *Artifact) Upload(buck *blob.Bucket) error {
return err
}
fmt.Printf("tag -> %s\n", remoteTagPath)
return nil
}
// Update manifest
manifest := a.UpdateManifest()
mjson, err := json.Marshal(manifest)
//--- Manage uploaded artifacts, list them, etc.
type ArtifactDescriptor struct {
Tag string
Date time.Time
}
type ArtifactManager struct {
name string
buck *blob.Bucket
artifacts []ArtifactDescriptor
}
func NewArtifactManager(buck *blob.Bucket, name string) *ArtifactManager {
return &ArtifactManager{
buck: buck,
name: name,
}
}
func (am *ArtifactManager) Scan() error {
iter := am.buck.List(&blob.ListOptions{
Prefix: fmt.Sprintf("df-dist-v1/%s/", am.name),
Delimiter: "/",
})
for {
obj, err := iter.Next(context.Background())
if err == io.EOF {
break
}
if err != nil {
return err
}
if obj.IsDir {
continue
}
ksplit := strings.Split(obj.Key, "/")
if len(ksplit) < 1 {
return errors.New(fmt.Sprintf("Invalid key name %s", obj.Key))
}
fname := ksplit[len(ksplit)-1]
ad := ArtifactDescriptor {
Tag: fname,
Date: obj.ModTime,
}
am.artifacts = append(am.artifacts, ad)
}
return nil
}
func (am *ArtifactManager) TagList() TagList {
// Sort by date desc
sort.Slice(am.artifacts, func(i, j int) bool {
return am.artifacts[i].Date.After(am.artifacts[j].Date)
})
// Build tagList
tagList := TagList {
Name: am.name,
}
for _, art := range am.artifacts {
tagList.Tags = append(tagList.Tags, art.Tag)
}
return tagList
}
func (am *ArtifactManager) UpdateTagList() error {
fmt.Printf("--- update taglist ---\n")
err := am.Scan()
if err != nil {
return err
}
remoteManifestPath := path.Join(prefix, a.Name)
if err := NewUploadFromByte(buck, mjson).UploadTo(remoteManifestPath); err != nil {
tagList := am.TagList()
fmt.Printf("computed tag list: %v\n", tagList)
txt, err := json.Marshal(tagList)
if err != nil {
return err
}
fmt.Printf("manifest -> %s\n", remoteManifestPath)
dst := path.Join("df-dist-v1", am.name)
err = NewUploadFromByte(am.buck, txt).ContentType("application/json").UploadTo(dst)
if err != nil {
return err
}
fmt.Printf("taglist -> %s\n", dst)
return nil
}
@ -313,6 +383,14 @@ var publishCmd = &cobra.Command{
os.Exit(1)
}
// update tag list
am := NewArtifactManager(bucket, art.Name)
err = am.UpdateTagList()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("✅ push succeeded\n")
},
}