Fix issues (#931)
* fix #929 * fix #925 * add test case * update model README * fix #929 * fix #929 * fix #929 * refactor dir * Adding todo comments Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>master
parent
a523ab1f93
commit
189721da16
@ -0,0 +1,59 @@
|
|||||||
|
package env
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetGoctlVersion obtains from the environment variable GOCTL_VERSION, prior to 1.1.11,
|
||||||
|
// the goctl version was 1.1.10 by default.
|
||||||
|
// the goctl version is set at runtime in the environment variable GOCTL_VERSION,
|
||||||
|
// see the detail at https://github.com/tal-tech/go-zero/blob/master/tools/goctl/goctl.go
|
||||||
|
func GetGoctlVersion() string {
|
||||||
|
currentVersion := os.Getenv("GOCTL_VERSION")
|
||||||
|
if currentVersion == "" {
|
||||||
|
currentVersion = "1.1.10"
|
||||||
|
}
|
||||||
|
return currentVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
var tag = map[string]int{"pre-alpha": 0, "alpha": 1, "pre-bata": 2, "beta": 3, "released": 4, "": 5}
|
||||||
|
|
||||||
|
// IsVersionGatherThan compares whether the current goctl version
|
||||||
|
// is gather than the target version
|
||||||
|
func IsVersionGatherThan(version, target string) bool {
|
||||||
|
versionNumber, versionTag := convertVersion(version)
|
||||||
|
targetVersionNumber, targetTag := convertVersion(target)
|
||||||
|
if versionNumber > targetVersionNumber {
|
||||||
|
return true
|
||||||
|
} else if versionNumber < targetVersionNumber {
|
||||||
|
return false
|
||||||
|
} else { // unchecked case, in normal, the goctl version does not contains suffix in release.
|
||||||
|
return tag[versionTag] > tag[targetTag]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// version format: number[.number]*(-tag)
|
||||||
|
func convertVersion(version string) (versionNumber float64, tag string) {
|
||||||
|
splits := strings.Split(version, "-")
|
||||||
|
tag = strings.Join(splits[1:], "")
|
||||||
|
var flag bool
|
||||||
|
numberStr := strings.Map(func(r rune) rune {
|
||||||
|
if r >= '0' && r <= '9' {
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
if r == '.' {
|
||||||
|
if flag {
|
||||||
|
return '_'
|
||||||
|
}
|
||||||
|
flag = true
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
return '_'
|
||||||
|
}, splits[0])
|
||||||
|
numberStr = strings.ReplaceAll(numberStr, "_", "")
|
||||||
|
versionNumber, _ = json.Number(numberStr).Float64()
|
||||||
|
return
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package env
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_convertVersion(t *testing.T) {
|
||||||
|
number, tag := convertVersion("1.1.10")
|
||||||
|
assert.Equal(t, 1.110, number)
|
||||||
|
assert.Equal(t, "", tag)
|
||||||
|
|
||||||
|
number, tag = convertVersion("0.1.11")
|
||||||
|
assert.Equal(t, 0.111, number)
|
||||||
|
assert.Equal(t, "", tag)
|
||||||
|
|
||||||
|
number, tag = convertVersion("1.11-pre")
|
||||||
|
assert.Equal(t, 1.11, number)
|
||||||
|
assert.Equal(t, "pre", tag)
|
||||||
|
|
||||||
|
number, tag = convertVersion("1.11-beta-v1")
|
||||||
|
assert.Equal(t, 1.11, number)
|
||||||
|
assert.Equal(t, "betav1", tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_IsVersionGatherThan(t *testing.T) {
|
||||||
|
assert.False(t, IsVersionGatherThan("0.11", "1.1"))
|
||||||
|
assert.True(t, IsVersionGatherThan("0.112", "0.1"))
|
||||||
|
assert.True(t, IsVersionGatherThan("1.1.10", "1.0.111"))
|
||||||
|
assert.True(t, IsVersionGatherThan("1.1.10", "1.1.10-pre"))
|
||||||
|
assert.True(t, IsVersionGatherThan("1.1.11-pre", "1.1.10"))
|
||||||
|
}
|
Loading…
Reference in New Issue