forked from fluxcd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |