-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from essentialkaos/develop
Release 4
- Loading branch information
Showing
6 changed files
with
277 additions
and
15 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
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,9 @@ | ||
// Auxiliary packages for Go | ||
package ek | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2009-2015 Essential Kaos // | ||
// Essential Kaos Open Source License <http://essentialkaos.com/ekol?en> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // |
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
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
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,131 @@ | ||
// Package provides methods for parsing semver version info | ||
package version | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2009-2015 Essential Kaos // | ||
// Essential Kaos Open Source License <http://essentialkaos.com/ekol?en> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
type Version struct { | ||
raw string | ||
versionSlice []int | ||
preRelease string | ||
build string | ||
} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func Parse(ver string) *Version { | ||
if ver == "" { | ||
return &Version{} | ||
} | ||
|
||
result := &Version{raw: ver} | ||
|
||
if strings.Contains(ver, "+") { | ||
bs := strings.Split(ver, "+") | ||
|
||
if bs[1] == "" { | ||
ver = bs[0] | ||
} else { | ||
ver = bs[0] | ||
result.build = bs[1] | ||
} | ||
} | ||
|
||
if strings.Contains(ver, "-") { | ||
ps := strings.Split(ver, "-") | ||
|
||
if ps[1] == "" { | ||
ver = ps[0] | ||
} else { | ||
ver = ps[0] | ||
result.preRelease = ps[1] | ||
} | ||
} | ||
|
||
for _, version := range strings.Split(ver, ".") { | ||
iv, err := strconv.Atoi(version) | ||
|
||
if err != nil { | ||
break | ||
} | ||
|
||
result.versionSlice = append(result.versionSlice, iv) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// Major return major version | ||
func (v *Version) Major() int { | ||
if v == nil || v.raw == "" || len(v.versionSlice) == 0 { | ||
return -1 | ||
} | ||
|
||
return v.versionSlice[0] | ||
} | ||
|
||
// Minor return minor version | ||
func (v *Version) Minor() int { | ||
if v == nil || v.raw == "" || len(v.versionSlice) == 0 { | ||
return -1 | ||
} | ||
|
||
if len(v.versionSlice) == 1 { | ||
return 0 | ||
} | ||
|
||
return v.versionSlice[1] | ||
} | ||
|
||
// Patch return patch version | ||
func (v *Version) Patch() int { | ||
if v == nil || v.raw == "" || len(v.versionSlice) == 0 { | ||
return -1 | ||
} | ||
|
||
if len(v.versionSlice) <= 2 { | ||
return 0 | ||
} | ||
|
||
return v.versionSlice[2] | ||
} | ||
|
||
// PreRelease return prerelease version | ||
func (v *Version) PreRelease() string { | ||
if v == nil || v.raw == "" { | ||
return "" | ||
} | ||
|
||
return v.preRelease | ||
} | ||
|
||
// Build return build | ||
func (v *Version) Build() string { | ||
if v == nil || v.raw == "" { | ||
return "" | ||
} | ||
|
||
return v.build | ||
} | ||
|
||
// String return version as string | ||
func (v *Version) String() string { | ||
if v == nil { | ||
return "" | ||
} | ||
|
||
return v.raw | ||
} |
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,110 @@ | ||
package version | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2009-2015 Essential Kaos // | ||
// Essential Kaos Open Source License <http://essentialkaos.com/ekol?en> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"testing" | ||
|
||
. "gopkg.in/check.v1" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
type VersionSuite struct{} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
var _ = Suite(&VersionSuite{}) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func (s *VersionSuite) TestMajor(c *C) { | ||
c.Assert(Parse("1").Major(), Equals, 1) | ||
c.Assert(Parse("2.0.0").Major(), Equals, 2) | ||
c.Assert(Parse("3.4.5").Major(), Equals, 3) | ||
c.Assert(Parse("4-beta1").Major(), Equals, 4) | ||
c.Assert(Parse("5-beta2+exp.sha.5114f85").Major(), Equals, 5) | ||
} | ||
|
||
func (s *VersionSuite) TestMinor(c *C) { | ||
c.Assert(Parse("1").Minor(), Equals, 0) | ||
c.Assert(Parse("2.1").Minor(), Equals, 1) | ||
c.Assert(Parse("3.4.5").Minor(), Equals, 4) | ||
c.Assert(Parse("4-alpha1").Minor(), Equals, 0) | ||
c.Assert(Parse("5-beta1+sha:5114f85").Minor(), Equals, 0) | ||
c.Assert(Parse("6.12.1-beta2+exp.sha.5114f85").Minor(), Equals, 12) | ||
} | ||
|
||
func (s *VersionSuite) TestPatch(c *C) { | ||
c.Assert(Parse("1").Patch(), Equals, 0) | ||
c.Assert(Parse("2.1").Patch(), Equals, 0) | ||
c.Assert(Parse("3.4.5").Patch(), Equals, 5) | ||
c.Assert(Parse("4-alpha1").Patch(), Equals, 0) | ||
c.Assert(Parse("5-beta1+sha:5114f85").Patch(), Equals, 0) | ||
c.Assert(Parse("6.12.1-beta2+exp.sha.5114f85").Patch(), Equals, 1) | ||
} | ||
|
||
func (s *VersionSuite) TestPre(c *C) { | ||
c.Assert(Parse("1").PreRelease(), Equals, "") | ||
c.Assert(Parse("2.1").PreRelease(), Equals, "") | ||
c.Assert(Parse("3.4.5").PreRelease(), Equals, "") | ||
c.Assert(Parse("3.4.5-").PreRelease(), Equals, "") | ||
c.Assert(Parse("4-alpha1").PreRelease(), Equals, "alpha1") | ||
c.Assert(Parse("5-beta1+sha:5114f85").PreRelease(), Equals, "beta1") | ||
c.Assert(Parse("6.12.1-beta2+exp.sha.5114f85").PreRelease(), Equals, "beta2") | ||
} | ||
|
||
func (s *VersionSuite) TestBuild(c *C) { | ||
c.Assert(Parse("1").Build(), Equals, "") | ||
c.Assert(Parse("2.1").Build(), Equals, "") | ||
c.Assert(Parse("3.4.5").Build(), Equals, "") | ||
c.Assert(Parse("4-alpha1+").Build(), Equals, "") | ||
c.Assert(Parse("5-beta1+sha:5114f85").Build(), Equals, "sha:5114f85") | ||
c.Assert(Parse("6.12.1-beta2+exp.sha.5114f85").Build(), Equals, "exp.sha.5114f85") | ||
} | ||
|
||
func (s *VersionSuite) TestString(c *C) { | ||
c.Assert(Parse("1").String(), Equals, "1") | ||
c.Assert(Parse("2.1").String(), Equals, "2.1") | ||
c.Assert(Parse("3.4.5").String(), Equals, "3.4.5") | ||
c.Assert(Parse("5-beta1+sha:5114f85").String(), Equals, "5-beta1+sha:5114f85") | ||
c.Assert(Parse("6.12.1-beta2+exp.sha.5114f85").String(), Equals, "6.12.1-beta2+exp.sha.5114f85") | ||
} | ||
|
||
func (s *VersionSuite) TestErrors(c *C) { | ||
var v1 *Version | ||
var v2 = &Version{} | ||
|
||
c.Assert(Parse("A").Major(), Equals, -1) | ||
c.Assert(Parse("A").Minor(), Equals, -1) | ||
c.Assert(Parse("A").Patch(), Equals, -1) | ||
c.Assert(Parse("A").PreRelease(), Equals, "") | ||
c.Assert(Parse("A").Build(), Equals, "") | ||
|
||
c.Assert(Parse("").Major(), Equals, -1) | ||
c.Assert(Parse("").Minor(), Equals, -1) | ||
c.Assert(Parse("").Patch(), Equals, -1) | ||
c.Assert(Parse("").PreRelease(), Equals, "") | ||
c.Assert(Parse("").Build(), Equals, "") | ||
|
||
c.Assert(v1.Major(), Equals, -1) | ||
c.Assert(v1.Minor(), Equals, -1) | ||
c.Assert(v1.Patch(), Equals, -1) | ||
c.Assert(v1.PreRelease(), Equals, "") | ||
c.Assert(v1.Build(), Equals, "") | ||
c.Assert(v1.String(), Equals, "") | ||
|
||
c.Assert(v2.Major(), Equals, -1) | ||
c.Assert(v2.Minor(), Equals, -1) | ||
c.Assert(v2.Patch(), Equals, -1) | ||
c.Assert(v2.PreRelease(), Equals, "") | ||
c.Assert(v2.Build(), Equals, "") | ||
c.Assert(v2.String(), Equals, "") | ||
} |