修复待升级节点版本号对比错误

This commit is contained in:
刘祥超
2021-01-18 12:35:29 +08:00
parent 5770408a85
commit 46914b3eb0
5 changed files with 77 additions and 4 deletions

19
internal/utils/ip.go Normal file
View File

@@ -0,0 +1,19 @@
package utils
import (
"encoding/binary"
"net"
)
// 将IP转换为整型
func IP2Long(ip string) uint32 {
s := net.ParseIP(ip)
if s == nil {
return 0
}
if len(s) == 16 {
return binary.BigEndian.Uint32(s[12:16])
}
return binary.BigEndian.Uint32(s)
}

18
internal/utils/version.go Normal file
View File

@@ -0,0 +1,18 @@
package utils
import (
"strings"
)
// 计算版本代号
func VersionToLong(version string) uint32 {
countDots := strings.Count(version, ".")
if countDots == 2 {
version += ".0"
} else if countDots == 1 {
version += ".0.0"
} else if countDots == 0 {
version += ".0.0.0"
}
return IP2Long(version)
}

View File

@@ -0,0 +1,31 @@
package utils
import "testing"
func TestNodeStatus_ComputerBuildVersionCode(t *testing.T) {
{
t.Log("", VersionToLong(""))
}
{
t.Log("0.0.6", VersionToLong("0.0.6"))
}
{
t.Log("0.0.6.1", VersionToLong("0.0.6.1"))
}
{
t.Log("0.0.7", VersionToLong("0.0.7"))
}
{
t.Log("0.7", VersionToLong("0.7"))
}
{
t.Log("7", VersionToLong("7"))
}
{
t.Log("7.0.1", VersionToLong("7.0.1"))
}
}