fix(versionning): Fix version
Version should be x.y.z for releases. It makes things homogenous for debian version (that needs a number as first char) and it is semantically better. At this time, the Makefile is OK. Maybe we don't need `PKG_VERSION` later.
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Version is the version of katenary. It is set at compile time.
|
||||
@@ -12,13 +12,19 @@ var Version = "master" // changed at compile time
|
||||
// the version is set at compile time for the github release. But, it the user get
|
||||
// katneary using `go install`, the version should be different.
|
||||
func GetVersion() string {
|
||||
if strings.HasPrefix(Version, "release-") {
|
||||
// try to get the semantic version from the Version variable (theorically set at compile time)
|
||||
reg := regexp.MustCompile(`^(.?\d+.\d+.\d+.*)|^release-.*`)
|
||||
if reg.MatchString(Version) {
|
||||
return Version
|
||||
}
|
||||
// get the version from the build info
|
||||
|
||||
// OK... let's try to get the version from the build info
|
||||
// get the version from the build info (when installed with go install)
|
||||
v, ok := debug.ReadBuildInfo()
|
||||
if ok {
|
||||
return v.Main.Version + "-" + v.GoVersion
|
||||
}
|
||||
|
||||
// OK... none worked, so we return the default version
|
||||
return Version
|
||||
}
|
||||
|
@@ -8,11 +8,24 @@ import (
|
||||
func TestVersion(t *testing.T) {
|
||||
// we build on "devel" branch
|
||||
v := GetVersion()
|
||||
// by default, the version comes from build info and it's a development version
|
||||
if !strings.Contains(v, "(devel)") {
|
||||
t.Errorf("Expected version to be set, got %s", v)
|
||||
}
|
||||
|
||||
// now, imagine we are on a release branch
|
||||
Version = "1.0.0"
|
||||
v = GetVersion()
|
||||
if !strings.Contains(v, "1.0.0") {
|
||||
t.Errorf("Expected version to be set, got %s", v)
|
||||
}
|
||||
// now, imagine we are on v1.0.0
|
||||
Version = "v1.0.0"
|
||||
v = GetVersion()
|
||||
if !strings.Contains(v, "v1.0.0") {
|
||||
t.Errorf("Expected version to be set, got %s", v)
|
||||
}
|
||||
// we can also compile a release branch
|
||||
Version = "release-1.0.0"
|
||||
v = GetVersion()
|
||||
if !strings.Contains(v, "release-1.0.0") {
|
||||
|
Reference in New Issue
Block a user