Skip to content

Commit

Permalink
fix(manifest): Sort index manifest entries after update
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Jung <alex@unikraft.io>
  • Loading branch information
nderjung committed Jan 8, 2025
1 parent 08e8727 commit 5b1563f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/LastPossum/kamino v0.0.2
github.com/MakeNowJust/heredoc v1.0.0
github.com/Masterminds/semver v1.5.0
github.com/Masterminds/semver/v3 v3.3.1
github.com/acorn-io/baaah v0.0.0-20230522221318-afcc93619e30
github.com/anchore/stereoscope v0.0.11
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/LastPossum/kamino v0.0.2 h1:Zry5lS7x7TTU1hzzk3Utnp+rX8kk/wWhuW52Ha9As
github.com/LastPossum/kamino v0.0.2/go.mod h1:H8Qm+6DGeNOoXk9hHIOEAQWS9nbo0YwK32pC/7REsOE=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7rj+4nv4=
github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
Expand Down
39 changes: 39 additions & 0 deletions manifest/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"unicode"

"github.com/Masterminds/semver"
"github.com/gobwas/glob"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -121,6 +123,43 @@ func (m *manifestManager) update(ctx context.Context) (*ManifestIndex, error) {
index.Manifests = append(index.Manifests, manifests...)
}

// Sort manifests by name
sort.Slice(index.Manifests, func(i, j int) bool {
return index.Manifests[i].Name < index.Manifests[j].Name
})

for i := range index.Manifests {
// Sort manifest versions by version
sort.Slice(index.Manifests[i].Versions, func(j, k int) bool {
jstr := index.Manifests[i].Versions[j].Version
if !strings.HasPrefix(jstr, "v") {
jstr = "v" + jstr
}

jSemVer, err := semver.NewVersion(jstr)
if err != nil {
return index.Manifests[i].Versions[j].Version < index.Manifests[i].Versions[k].Version
}

kstr := index.Manifests[i].Versions[j].Version
if !strings.HasPrefix(kstr, "v") {
kstr = "v" + kstr
}

kSemVer, err := semver.NewVersion(kstr)
if err != nil {
return index.Manifests[i].Versions[j].Version < index.Manifests[i].Versions[k].Version
}

return jSemVer.LessThan(kSemVer)
})

// Sort manifest channels by name
sort.Slice(index.Manifests[i].Channels, func(j, k int) bool {
return index.Manifests[i].Channels[j].Name < index.Manifests[i].Channels[k].Name
})
}

return index, nil
}

Expand Down

0 comments on commit 5b1563f

Please sign in to comment.