Skip to content

Commit

Permalink
Introduce version package
Browse files Browse the repository at this point in the history
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

hiddeco committed Oct 19, 2020
1 parent 99278c9 commit 3164683
Showing 4 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions version/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/fluxcd/pkg/version

go 1.15

require github.com/Masterminds/semver/v3 v3.1.0
2 changes: 2 additions & 0 deletions version/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/Masterminds/semver/v3 v3.1.0 h1:Y2lUDsFKVRSYGojLJ1yLxSXdMmMYTYls0rCvoqmMUQk=
github.com/Masterminds/semver/v3 v3.1.0/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
33 changes: 33 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2020 The Flux CD contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package version

import (
"strings"

"github.com/Masterminds/semver/v3"
)

// ParseVersion behaves as semver.StrictNewVersion, with as sole exception
// that it allows versions with a preceding "v" (i.e. v1.2.3).
func ParseVersion(v string) (*semver.Version, error) {
vLessV := strings.TrimPrefix(v, "v")
if _, err := semver.StrictNewVersion(vLessV); err != nil {
return nil, err
}
return semver.NewVersion(v)
}
49 changes: 49 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2020 The Flux CD contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package version

import (
"testing"
)

func TestParseVersion(t *testing.T) {
tests := []struct {
version string
err bool
}{
{"v1.2.3", false},
{"v1.0", true},
{"v1", true},
{"v1.2.beta", true},
{"v1.2-5", true},
{"v1.2-beta.5", true},
{"\nv1.2", true},
{"v1.2.0-x.Y.0+metadata", false},
{"v1.2.0-x.Y.0+metadata-width-hypen", false},
{"v1.2.3-rc1-with-hypen", false},
{"v1.2.3.4", true},
}

for _, tc := range tests {
_, err := ParseVersion(tc.version)
if tc.err && err == nil {
t.Fatalf("expected error for version: %s", tc.version)
} else if !tc.err && err != nil {
t.Fatalf("error for version %s: %s", tc.version, err)
}
}
}

0 comments on commit 3164683

Please sign in to comment.