go_dreamfactory/cmd/v2/lib/common/utils.go
2022-08-12 18:53:45 +08:00

71 lines
1.2 KiB
Go

package common
import (
"bytes"
"encoding/json"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
func FormatJson(data string) (string, error) {
var out bytes.Buffer
err := json.Indent(&out, []byte(data), "", " ")
return out.String(), err
}
func SubStr(str string, start int, length int) (result string) {
s := []rune(str)
total := len(s)
if total == 0 {
return
}
// 允许从尾部开始计算
if start < 0 {
start = total + start
if start < 0 {
return
}
}
if start > total {
return
}
// 到末尾
if length < 0 {
length = total
}
end := start + length
if end > total {
result = string(s[start:])
} else {
result = string(s[start:end])
}
return
}
// 是否升级
func IsUpgrade(newVersion, oldVersion string) bool {
nvArr := strings.SplitN(newVersion, ".", 3)
if len(nvArr) != 3 {
logrus.Error("new version format err")
return false
}
ovArr := strings.SplitN(oldVersion, ".", 3)
if len(ovArr) != 3 {
logrus.Error("old version format err")
return false
}
nvNum := cast.ToInt(nvArr[0])*100 + cast.ToInt(nvArr[1])*10 + cast.ToInt(nvArr[2])
ovNum := cast.ToInt(ovArr[0])*100 + cast.ToInt(ovArr[1])*10 + cast.ToInt(ovArr[2])
if nvNum > ovNum {
return true
}
return false
}