Skip to content

Commit

Permalink
feat: 支持获取指定版本
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 23, 2023
1 parent 24a32c0 commit 12c25bf
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
3 changes: 2 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

## 卸载面板
Expand All @@ -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)
```

卸载面板前请务必备份好所有数据,提前卸载面板全部插件。卸载后数据将**无法恢复**
Expand Down
4 changes: 2 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!
Expand Down
105 changes: 97 additions & 8 deletions pkg/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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()
Expand Down
43 changes: 43 additions & 0 deletions pkg/tools/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions scripts/install_panel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 12c25bf

Please sign in to comment.