Unverified Commit 5ab07b01 authored by bryanl's avatar bryanl
Browse files

Support more version formats when sorting charts



Added the following formats:
* v1.2.3
* 1.2
* 1
Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent c05c4969
......@@ -17,9 +17,14 @@ package helm
import (
"path/filepath"
<<<<<<< HEAD
"strings"
=======
>>>>>>> 4417ebd4... update version sorting
"github.com/blang/semver"
"github.com/ksonnet/ksonnet/pkg/app"
"github.com/ksonnet/ksonnet/pkg/util/version"
"github.com/pkg/errors"
"github.com/spf13/afero"
)
......@@ -33,24 +38,27 @@ func LatestChartVersion(a app.App, repoName, chartName string) (string, error) {
return "", errors.Wrapf(err, "reading dir %q", path)
}
var versions semver.Versions
var versions []version.Version
for _, fi := range fis {
if !fi.IsDir() {
continue
}
v, err := semver.Make(fi.Name())
v, err := version.Make(fi.Name())
if err != nil {
continue
return "", err
}
versions = append(versions, v)
}
if len(versions) == 0 {
return "", errors.Errorf("chart %s/%s doesn't have any releases", repoName, chartName)
}
semver.Sort(versions)
return versions[0].String(), nil
version.Sort(versions)
lastestVersion := versions[len(versions)-1]
return lastestVersion.String(), nil
}
package version
import "sort"
// Versions represents multiple versions.
type Versions []Version
// Len returns length of version collection
func (s Versions) Len() int {
return len(s)
}
// Swap swaps two versions inside the collection by its indices
func (s Versions) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Less checks if version at index i is less than version at index j
func (s Versions) Less(i, j int) bool {
return s[i].v.LT(s[j].v)
}
// Sort sorts a slice of versions
func Sort(versions []Version) {
sort.Sort(Versions(versions))
}
package version
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSort(t *testing.T) {
versionNames := []string{
"6.5.1",
"0.1.3",
"11.3",
}
var versions []Version
for _, s := range versionNames {
v, err := Make(s)
require.NoError(t, err)
versions = append(versions, v)
}
Sort(versions)
var got []string
for _, v := range versions {
got = append(got, v.String())
}
expected := []string{"0.1.3", "6.5.1", "11.3"}
assert.Equal(t, expected, got)
}
package version
import (
"strings"
"github.com/blang/semver"
)
// Version represents a version.
type Version struct {
raw string
v semver.Version
}
// Make takes a string and converts it to a version. It supports the following:
// * 1.2.3
// * 1.2
// * 1
// * v1.2.3
// * 1.2.3-build-1
func Make(s string) (Version, error) {
versionStr := strings.TrimPrefix(s, "v")
parts := strings.SplitN(versionStr, ".", 3)
switch len(parts) {
case 3:
// nothing to do because we have three parts of a version
case 2:
// assume we have major and minor
versionStr = strings.Join(append(parts, "0"), ".")
case 1:
// assume we have major
versionStr = strings.Join(append(parts, "0", "0"), ".")
}
v, err := semver.Make(versionStr)
if err != nil {
return Version{}, err
}
return Version{
raw: s,
v: v,
}, nil
}
func (v *Version) String() string {
return v.raw
}
package version
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMake(t *testing.T) {
cases := []struct {
name string
in string
out string
isErr bool
}{
{
name: "semantic version",
in: "1.0.0",
out: "1.0.0",
},
{
name: "major.minor",
in: "1.0",
out: "1.0",
},
{
name: "major",
in: "1",
out: "1",
},
{
name: "version has v prefix",
in: "v1.2.3",
out: "v1.2.3",
},
{
name: "semantic version with additional info",
in: "1.2.3-beta.1",
out: "1.2.3-beta.1",
},
{
name: "empty version string",
in: "",
isErr: true,
},
{
name: "invalid version",
in: "invalid",
isErr: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
v, err := Make(tc.in)
if tc.isErr {
require.Error(t, err)
return
}
assert.Equal(t, tc.out, v.String())
})
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment