mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 06:47:20 +08:00
feat: 优化版本号处理
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/goravel/framework/contracts/http"
|
||||
"github.com/goravel/framework/facades"
|
||||
"github.com/hashicorp/go-version"
|
||||
|
||||
"github.com/TheTNB/panel/app/models"
|
||||
"github.com/TheTNB/panel/internal"
|
||||
@@ -250,13 +251,21 @@ func (r *InfoController) InstalledDbAndPhp(ctx http.Context) http.Response {
|
||||
|
||||
// CheckUpdate 检查面板更新
|
||||
func (r *InfoController) CheckUpdate(ctx http.Context) http.Response {
|
||||
version := facades.Config().GetString("panel.version")
|
||||
remote, err := tools.GetLatestPanelVersion()
|
||||
current := facades.Config().GetString("panel.version")
|
||||
latest, err := tools.GetLatestPanelVersion()
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, "获取最新版本失败")
|
||||
}
|
||||
|
||||
if tools.VersionCompare(version, remote.Version, ">=") {
|
||||
v1, err := version.NewVersion(current)
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, "版本号解析失败")
|
||||
}
|
||||
v2, err := version.NewVersion(latest.Version)
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, "版本号解析失败")
|
||||
}
|
||||
if v1.GreaterThanOrEqual(v2) {
|
||||
return Success(ctx, http.Json{
|
||||
"update": false,
|
||||
})
|
||||
@@ -269,17 +278,25 @@ func (r *InfoController) CheckUpdate(ctx http.Context) http.Response {
|
||||
|
||||
// UpdateInfo 获取更新信息
|
||||
func (r *InfoController) UpdateInfo(ctx http.Context) http.Response {
|
||||
version := facades.Config().GetString("panel.version")
|
||||
current, err := tools.GetLatestPanelVersion()
|
||||
current := facades.Config().GetString("panel.version")
|
||||
latest, err := tools.GetLatestPanelVersion()
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, "获取最新版本失败")
|
||||
}
|
||||
|
||||
if tools.VersionCompare(version, current.Version, ">=") {
|
||||
v1, err := version.NewVersion(current)
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, "版本号解析失败")
|
||||
}
|
||||
v2, err := version.NewVersion(latest.Version)
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, "版本号解析失败")
|
||||
}
|
||||
if v1.GreaterThanOrEqual(v2) {
|
||||
return Error(ctx, http.StatusInternalServerError, "当前版本已是最新版本")
|
||||
}
|
||||
|
||||
versions, err := tools.GenerateVersions(version, current.Version)
|
||||
versions, err := tools.GenerateVersions(current, latest.Version)
|
||||
if err != nil {
|
||||
return Error(ctx, http.StatusInternalServerError, "获取更新信息失败")
|
||||
}
|
||||
|
||||
1
go.mod
1
go.mod
@@ -12,6 +12,7 @@ require (
|
||||
github.com/goravel/framework v1.14.1-0.20240627172353-91c3c46cf333
|
||||
github.com/goravel/gin v1.2.1
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/hashicorp/go-version v1.7.0
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/libdns/alidns v1.0.3
|
||||
github.com/libdns/cloudflare v0.1.1
|
||||
|
||||
2
go.sum
2
go.sum
@@ -297,6 +297,8 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
|
||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
|
||||
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
|
||||
@@ -66,35 +66,6 @@ func GetMonitoringInfo() MonitoringInfo {
|
||||
return res
|
||||
}
|
||||
|
||||
// VersionCompare 版本比较
|
||||
func VersionCompare(ver1, ver2, operator string) bool {
|
||||
v1 := strings.TrimPrefix(ver1, "v")
|
||||
v2 := strings.TrimPrefix(ver2, "v")
|
||||
|
||||
v1s := strings.Split(v1, ".")
|
||||
v2s := strings.Split(v2, ".")
|
||||
|
||||
for len(v1s) < len(v2s) {
|
||||
v1s = append(v1s, "0")
|
||||
}
|
||||
|
||||
for len(v2s) < len(v1s) {
|
||||
v2s = append(v2s, "0")
|
||||
}
|
||||
|
||||
for i := 0; i < len(v1s); i++ {
|
||||
v1i := cast.ToInt(v1s[i])
|
||||
v2i := cast.ToInt(v2s[i])
|
||||
|
||||
if v1i > v2i {
|
||||
return operator == ">" || operator == ">=" || operator == "!="
|
||||
} else if v1i < v2i {
|
||||
return operator == "<" || operator == "<=" || operator == "!="
|
||||
}
|
||||
}
|
||||
return operator == "==" || operator == ">=" || operator == "<="
|
||||
}
|
||||
|
||||
// GenerateVersions 获取版本列表
|
||||
func GenerateVersions(start, end string) ([]string, error) {
|
||||
var versions []string
|
||||
|
||||
@@ -18,36 +18,6 @@ func (s *HelperTestSuite) TestGetMonitoringInfo() {
|
||||
s.NotNil(GetMonitoringInfo())
|
||||
}
|
||||
|
||||
func (s *HelperTestSuite) TestVersionCompare() {
|
||||
s.True(VersionCompare("1.0.0", "1.0.0", "=="))
|
||||
s.True(VersionCompare("1.0.0", "1.0.0", ">="))
|
||||
s.True(VersionCompare("1.0.0", "1.0.0", "<="))
|
||||
s.False(VersionCompare("1.0.0", "1.0.0", ">"))
|
||||
s.False(VersionCompare("1.0.0", "1.0.0", "<"))
|
||||
s.False(VersionCompare("1.0.0", "1.0.0", "!="))
|
||||
|
||||
s.True(VersionCompare("1.0.0", "1.0.1", "<"))
|
||||
s.True(VersionCompare("1.0.0", "1.0.1", "<="))
|
||||
s.True(VersionCompare("1.0.0", "1.0.1", "!="))
|
||||
s.False(VersionCompare("1.0.0", "1.0.1", "=="))
|
||||
s.False(VersionCompare("1.0.0", "1.0.1", ">="))
|
||||
s.False(VersionCompare("1.0.0", "1.0.1", ">"))
|
||||
|
||||
s.True(VersionCompare("1.0.1", "1.0.0", ">"))
|
||||
s.True(VersionCompare("1.0.1", "1.0.0", ">="))
|
||||
s.True(VersionCompare("1.0.1", "1.0.0", "!="))
|
||||
s.False(VersionCompare("1.0.1", "1.0.0", "=="))
|
||||
s.False(VersionCompare("1.0.1", "1.0.0", "<="))
|
||||
s.False(VersionCompare("1.0.1", "1.0.0", "<"))
|
||||
|
||||
s.True(VersionCompare("v1.0.0", "1.0.0", "=="))
|
||||
s.True(VersionCompare("1.0.0", "v1.0.0", "=="))
|
||||
s.True(VersionCompare("v1.0.0", "v1.0.0", "=="))
|
||||
s.False(VersionCompare("v2.2.9", "v2.2.10", ">="))
|
||||
s.True(VersionCompare("v2.2.10", "v2.2.9", ">="))
|
||||
|
||||
}
|
||||
|
||||
func (s *HelperTestSuite) TestGenerateVersions() {
|
||||
versions, err := GenerateVersions("1.0.0", "1.0.3")
|
||||
s.NoError(err)
|
||||
|
||||
Reference in New Issue
Block a user