osbuild / images

Image builder image definition library
Apache License 2.0
23 stars 52 forks source link

distrosort: add new `distrosort.Names` helper #1012

Closed mvo5 closed 1 week ago

mvo5 commented 2 weeks ago

[edit: alternative version of https://github.com/osbuild/images/pull/1000 - does not support -beta and similar semver features but reuses more of the existing infrastructure]

distrosort: add new distrosort.Names helper

This commit adds a new distrosort.Names() helper that can be used sort sort distro names. It will take version numbers of the distro into account and sort by semantic version. This is useful in e.g. user-interfaces where we want to present the version in a nicely sorted way.

Sorting follows the go-version library which supports semantic versioning [0] so anything supported there will be sorted correctly including -beta suffixes and similar (see the spec for details).

Note that this is an extra pkg because the distro and distroid would be circular imports otherwise. The alternative might be to create a distro.ID package.

[0] https://semver.org/


distro: add new 'ID.Version()` method

This commit adds a new method ID.Version() that returns a go-version.Version object so that we can easily sort the distro versions.

The ID.Version() currently also has to return an error because the ID struct exposes MajorVersion/MinorVersion which means that during the lifetime the Version may become invalid even after ParseID was run. We could fix this by making {Major,Minor}Version getters instead of the current way. Then we could also use the version.NewVersion() library directy to parse the version string from the ID and support things like -beta or other extras that semantic versioning allows. But then that may interfere with the distroid.Parser interface so definitely needs some more thinking.