From 12c25bfcab11a28a9ab9dcdd97566b319994a4d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Mon, 23 Oct 2023 19:39:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/goreleaser.yml | 2 +- .github/workflows/test.yml | 2 +- .gitlab-ci.yml | 3 +- README.md | 4 +- README_EN.md | 4 +- pkg/tools/tools.go | 105 ++++++++++++++++++++++++++++--- pkg/tools/tools_test.go | 43 +++++++++++++ scripts/install_panel.sh | 8 +-- 8 files changed, 152 insertions(+), 19 deletions(-) diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml index 393c9210..9212099e 100644 --- a/.github/workflows/goreleaser.yml +++ b/.github/workflows/goreleaser.yml @@ -20,7 +20,7 @@ jobs: - name: Fetch Frontend run: | sudo apt-get install -y curl jq unzip zip - curl -s https://api.github.com/repos/haozi-team/panel-frontend/releases/latest | jq -r ".assets[] | select(.name | contains(\"dist\")) | .browser_download_url" | xargs curl -L -o frontend.zip + curl -L -s https://api.github.com/repos/haozi-team/panel-frontend/releases/latest | jq -r ".assets[] | select(.name | contains(\"dist\")) | .browser_download_url" | xargs curl -L -o frontend.zip rm -rf public unzip frontend.zip mv dist public diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cf7a5268..4ca08cf4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,6 +13,6 @@ jobs: with: go-version: 'stable' - name: Install dependencies - run: go mod tidy + run: sudo apt-get install -y curl jq - name: Run tests run: go test ./... diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 265676a2..2e622eb5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -38,6 +38,7 @@ unit_test: extends: .go_cache allow_failure: true script: + - apt-get install -y curl jq - go test -v -coverprofile=coverage.txt -covermode=atomic ./... build: @@ -59,7 +60,7 @@ fetch: - sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories - apk add --no-cache curl jq unzip zip script: - - curl -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel-frontend/releases" | jq -r '.[0].assets.links[] | select(.name | contains("dist")) | .direct_asset_url' | xargs curl -L -o frontend.zip + - curl -L -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel-frontend/releases" | jq -r '.[0].assets.links[] | select(.name | contains("dist")) | .direct_asset_url' | xargs curl -L -o frontend.zip - rm -rf public - unzip frontend.zip - mv dist public diff --git a/README.md b/README.md index 28cfdcf7..f55099e7 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ CentOS Stream 可使用迁移脚本迁移至支持的系统: [CentOS 8/9 迁移 如果你决定继续,请以`root`用户登录服务器,执行以下命令安装面板: ```shell -bash <(curl -sSL https://jihulab.com/haozi-team/download/-/raw/main/panel/install_panel.sh) +bash <(curl -L -sSL https://jihulab.com/haozi-team/download/-/raw/main/panel/install_panel.sh) ``` ## 卸载面板 @@ -62,7 +62,7 @@ bash <(curl -sSL https://jihulab.com/haozi-team/download/-/raw/main/panel/instal 如果你无法重装系统,请以`root`用户登录服务器,执行以下命令卸载面板: ```shell -bash <(curl -sSL https://jihulab.com/haozi-team/download/-/raw/main/panel/uninstall_panel.sh) +bash <(curl -L -sSL https://jihulab.com/haozi-team/download/-/raw/main/panel/uninstall_panel.sh) ``` 卸载面板前请务必备份好所有数据,提前卸载面板全部插件。卸载后数据将**无法恢复**! diff --git a/README_EN.md b/README_EN.md index 49915617..148423d6 100644 --- a/README_EN.md +++ b/README_EN.md @@ -48,7 +48,7 @@ Before installing the panel, you need to understand the basic knowledge of the L If you decide to continue, please log in to the server as `root` user and execute the following command to install the panel: ```shell -bash <(curl -sSL https://jihulab.com/haozi-team/download/-/raw/main/panel/install_panel.sh) +bash <(curl -L -sSL https://jihulab.com/haozi-team/download/-/raw/main/panel/install_panel.sh) ``` ## Uninstall Panel @@ -58,7 +58,7 @@ Recommended to back up data and reinstall the system first, so that the system c If you are unable to reinstall the system, log in to the server as the `root` user and execute the following command to uninstall the panel: ```shell -bash <(curl -sSL https://jihulab.com/haozi-team/download/-/raw/main/panel/uninstall_panel.sh) +bash <(curl -L -sSL https://jihulab.com/haozi-team/download/-/raw/main/panel/uninstall_panel.sh) ``` Before uninstalling the panel, please be sure to back up all data and uninstall all panel plugins in advance. The data will **not be recoverable** after uninstallation! diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go index 14cd4b97..7977ca2d 100644 --- a/pkg/tools/tools.go +++ b/pkg/tools/tools.go @@ -56,6 +56,32 @@ 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++ { + if v1s[i] > v2s[i] { + return operator == ">" || operator == ">=" || operator == "!=" + } else if v1s[i] < v2s[i] { + return operator == "<" || operator == "<=" || operator == "!=" + } + } + return operator == "==" || operator == ">=" || operator == "<=" +} + type PanelInfo struct { Name string `json:"name"` Version string `json:"version"` @@ -71,9 +97,9 @@ func GetLatestPanelVersion() (PanelInfo, error) { isChina := IsChina() if isChina { - output = Exec(`curl -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel/releases"`) + output = Exec(`curl -L -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel/releases/permalink/latest"`) } else { - output = Exec(`curl -s "https://api.github.com/repos/haozi-team/panel/releases/latest"`) + output = Exec(`curl -L -s "https://api.github.com/repos/haozi-team/panel/releases/latest"`) } if len(output) == 0 { @@ -97,14 +123,14 @@ func GetLatestPanelVersion() (PanelInfo, error) { var name, version, body, date, downloadUrl string if isChina { - name = Exec("jq -r '.[0].name' " + fileName) - version = Exec("jq -r '.[0].tag_name' " + fileName) - body = Exec("jq -r '.[0].description' " + fileName) - date = Exec("jq -r '.[0].created_at' " + fileName) + name = Exec("jq -r '.name' " + fileName) + version = Exec("jq -r '.tag_name' " + fileName) + body = Exec("jq -r '.description' " + fileName) + date = Exec("jq -r '.created_at' " + fileName) if IsArm() { - downloadUrl = Exec("jq -r '.[0].assets.links[] | select(.name | contains(\"arm64\")) | .direct_asset_url' " + fileName) + downloadUrl = Exec("jq -r '.assets.links[] | select(.name | contains(\"arm64\")) | .direct_asset_url' " + fileName) } else { - downloadUrl = Exec("jq -r '.[0].assets.links[] | select(.name | contains(\"amd64v2\")) | .direct_asset_url' " + fileName) + downloadUrl = Exec("jq -r '.assets.links[] | select(.name | contains(\"amd64v2\")) | .direct_asset_url' " + fileName) } } else { name = Exec("jq -r '.name' " + fileName) @@ -127,6 +153,69 @@ func GetLatestPanelVersion() (PanelInfo, error) { return info, nil } +// GetPanelVersion 获取指定面板版本 +func GetPanelVersion(version string) (PanelInfo, error) { + var info PanelInfo + var output string + isChina := IsChina() + + if isChina { + output = Exec(`curl -L -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel/releases/"` + version + `"`) + } else { + output = Exec(`curl -L -s "https://api.github.com/repos/haozi-team/panel/releases/tags/` + version + `"`) + } + + if len(output) == 0 { + return info, errors.New("获取面板版本失败") + } + + file, err := os.CreateTemp("", "panel") + if err != nil { + return info, errors.New("创建临时文件失败") + } + defer os.Remove(file.Name()) + _, err = file.Write([]byte(output)) + if err != nil { + return info, errors.New("写入临时文件失败") + } + err = file.Close() + if err != nil { + return info, errors.New("关闭临时文件失败") + } + fileName := file.Name() + + var name, version2, body, date, downloadUrl string + if isChina { + name = Exec("jq -r '.name' " + fileName) + version2 = Exec("jq -r '.tag_name' " + fileName) + body = Exec("jq -r '.description' " + fileName) + date = Exec("jq -r '.created_at' " + fileName) + if IsArm() { + downloadUrl = Exec("jq -r '.assets.links[] | select(.name | contains(\"arm64\")) | .direct_asset_url' " + fileName) + } else { + downloadUrl = Exec("jq -r '.assets.links[] | select(.name | contains(\"amd64v2\")) | .direct_asset_url' " + fileName) + } + } else { + name = Exec("jq -r '.name' " + fileName) + version2 = Exec("jq -r '.tag_name' " + fileName) + body = Exec("jq -r '.body' " + fileName) + date = Exec("jq -r '.published_at' " + fileName) + if IsArm() { + downloadUrl = Exec("jq -r '.assets[] | select(.name | contains(\"arm64\")) | .browser_download_url' " + fileName) + } else { + downloadUrl = Exec("jq -r '.assets[] | select(.name | contains(\"amd64v2\")) | .browser_download_url' " + fileName) + } + } + + info.Name = name + info.Version = version2 + info.Body = body + info.Date = date + info.DownloadUrl = downloadUrl + + return info, nil +} + // UpdatePanel 更新面板 func UpdatePanel() error { panelInfo, err := GetLatestPanelVersion() diff --git a/pkg/tools/tools_test.go b/pkg/tools/tools_test.go index eceebb14..553a303f 100644 --- a/pkg/tools/tools_test.go +++ b/pkg/tools/tools_test.go @@ -17,3 +17,46 @@ func TestHelperTestSuite(t *testing.T) { 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", "!=")) + + // 测试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.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", ">")) + + // 测试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.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", "<")) + + // 测试带有 'v' 前缀的版本号 + 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", "==")) +} + +func (s *HelperTestSuite) TestGetLatestPanelVersion() { + version, err := GetLatestPanelVersion() + s.NotEmpty(version) + s.Nil(err) +} + +func (s *HelperTestSuite) TestGetPanelVersion() { + version, err := GetPanelVersion("v2.0.58") + s.NotEmpty(version) + s.Nil(err) +} diff --git a/scripts/install_panel.sh b/scripts/install_panel.sh index bfd3713f..43c2acd3 100644 --- a/scripts/install_panel.sh +++ b/scripts/install_panel.sh @@ -181,15 +181,15 @@ Init_Panel() { # 下载面板zip包并解压 if [ "${ARCH}" == "x86_64" ]; then if ${inChina}; then - panelZip=$(curl -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel/releases" | jq -r '.[0].assets.links[] | select(.name | contains("amd64v2")) | .direct_asset_url') + panelZip=$(curl -L -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel/releases" | jq -r '.[0].assets.links[] | select(.name | contains("amd64v2")) | .direct_asset_url') else - panelZip=$(curl -s "https://api.github.com/repos/haozi-team/panel/releases/latest" | jq -r '.assets[] | select(.name | contains("amd64v2")) | .browser_download_url') + panelZip=$(curl -L -s "https://api.github.com/repos/haozi-team/panel/releases/latest" | jq -r '.assets[] | select(.name | contains("amd64v2")) | .browser_download_url') fi elif [ "${ARCH}" == "aarch64" ]; then if ${inChina}; then - panelZip=$(curl -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel/releases" | jq -r '.[0].assets.links[] | select(.name | contains("arm64")) | .direct_asset_url') + panelZip=$(curl -L -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel/releases" | jq -r '.[0].assets.links[] | select(.name | contains("arm64")) | .direct_asset_url') else - panelZip=$(curl -s "https://api.github.com/repos/haozi-team/panel/releases/latest" | jq -r '.assets[] | select(.name | contains("arm64")) | .browser_download_url') + panelZip=$(curl -L -s "https://api.github.com/repos/haozi-team/panel/releases/latest" | jq -r '.assets[] | select(.name | contains("arm64")) | .browser_download_url') fi else echo -e $HR