Skip to content

Commit

Permalink
distro: add new 'ID.Version()` method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvo5 committed Nov 6, 2024
1 parent 5dcc752 commit 5a54cb7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/distro/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strconv"
"strings"

"github.com/hashicorp/go-version"
)

// ID represents a distro name and version
Expand All @@ -22,6 +24,16 @@ func (id ID) String() string {
return fmt.Sprintf("%s-%d.%d", id.Name, id.MajorVersion, id.MinorVersion)
}

func (id ID) Version() (*version.Version, error) {
var verStr string
if id.MinorVersion == -1 {
verStr = fmt.Sprintf("%d", id.MajorVersion)
} else {
verStr = fmt.Sprintf("%d.%d", id.MajorVersion, id.MinorVersion)
}
return version.NewVersion(verStr)
}

type ParseError struct {
ToParse string
Msg string
Expand Down
7 changes: 7 additions & 0 deletions pkg/distro/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func TestDistroIDParser(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, id)
assert.Equal(t, tc.expected, id)

ver, err := id.Version()
assert.NoError(t, err)
assert.Equal(t, ver.Segments()[0], tc.expected.MajorVersion)
if tc.expected.MinorVersion > -1 {
assert.Equal(t, ver.Segments()[1], tc.expected.MinorVersion)
}
})
}

Expand Down

0 comments on commit 5a54cb7

Please sign in to comment.