mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 11:27:17 +08:00
refactor: tools
This commit is contained in:
@@ -9,8 +9,8 @@ import (
|
||||
"github.com/goravel/framework/facades"
|
||||
)
|
||||
|
||||
// WriteFile 写入文件
|
||||
func WriteFile(path string, data string, permission os.FileMode) bool {
|
||||
// Write 写入文件
|
||||
func Write(path string, data string, permission os.FileMode) bool {
|
||||
if err := os.MkdirAll(filepath.Dir(path), permission); err != nil {
|
||||
facades.Log().Errorf("[面板][Helpers] 创建目录失败: %s", err.Error())
|
||||
return false
|
||||
@@ -25,8 +25,8 @@ func WriteFile(path string, data string, permission os.FileMode) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ReadFile 读取文件
|
||||
func ReadFile(path string) string {
|
||||
// Read 读取文件
|
||||
func Read(path string) string {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
facades.Log().Errorf("[面板][Helpers] 读取文件 %s 失败: %s", path, err.Error())
|
||||
@@ -36,8 +36,8 @@ func ReadFile(path string) string {
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// RemoveFile 删除文件
|
||||
func RemoveFile(path string) bool {
|
||||
// Remove 删除文件
|
||||
func Remove(path string) bool {
|
||||
if err := os.Remove(path); err != nil {
|
||||
facades.Log().Errorf("[面板][Helpers] 删除文件 %s 失败: %s", path, err.Error())
|
||||
return false
|
||||
@@ -46,8 +46,8 @@ func RemoveFile(path string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ExecShell 执行 shell 命令
|
||||
func ExecShell(shell string) string {
|
||||
// Exec 执行 shell 命令
|
||||
func Exec(shell string) string {
|
||||
cmd := exec.Command("bash", "-c", shell)
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
@@ -59,8 +59,8 @@ func ExecShell(shell string) string {
|
||||
return strings.TrimSpace(string(output))
|
||||
}
|
||||
|
||||
// ExecShellAsync 异步执行 shell 命令
|
||||
func ExecShellAsync(shell string) {
|
||||
// ExecAsync 异步执行 shell 命令
|
||||
func ExecAsync(shell string) {
|
||||
cmd := exec.Command("bash", "-c", shell)
|
||||
|
||||
err := cmd.Start()
|
||||
@@ -126,33 +126,32 @@ func Empty(path string) bool {
|
||||
}
|
||||
|
||||
// Mv 移动路径
|
||||
func Mv(src, dst string) bool {
|
||||
func Mv(src, dst string) (bool, error) {
|
||||
cmd := exec.Command("mv", src, dst)
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
facades.Log().Errorf("[面板][Helpers] 移动 %s 到 %s 失败: %s", src, dst, err.Error())
|
||||
return false
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Cp 复制路径
|
||||
func Cp(src, dst string) bool {
|
||||
func Cp(src, dst string) (bool, error) {
|
||||
cmd := exec.Command("cp", "-r", src, dst)
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
facades.Log().Errorf("[面板][Helpers] 复制 %s 到 %s 失败: %s", src, dst, err.Error())
|
||||
return false
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Size 获取路径大小
|
||||
func Size(path string) int64 {
|
||||
func Size(path string) (int64, error) {
|
||||
var size int64
|
||||
|
||||
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
|
||||
@@ -160,20 +159,18 @@ func Size(path string) int64 {
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
facades.Log().Errorf("[面板][Helpers] 获取路径 %s 大小失败: %s", path, err.Error())
|
||||
return 0
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return size
|
||||
return size, nil
|
||||
}
|
||||
|
||||
// FileSize 获取文件大小
|
||||
func FileSize(path string) int64 {
|
||||
func FileSize(path string) (int64, error) {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
facades.Log().Errorf("[面板][Helpers] 获取文件 %s 大小失败: %s", path, err.Error())
|
||||
return 0
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return info.Size()
|
||||
return info.Size(), nil
|
||||
}
|
||||
|
||||
@@ -17,45 +17,45 @@ func TestSystemHelperTestSuite(t *testing.T) {
|
||||
suite.Run(t, &SystemHelperTestSuite{})
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestWriteFile() {
|
||||
func (s *SystemHelperTestSuite) TestWrite() {
|
||||
filePath := "/tmp/testfile"
|
||||
defer os.Remove(filePath)
|
||||
|
||||
s.True(WriteFile(filePath, "test data", 0644))
|
||||
s.True(Write(filePath, "test data", 0644))
|
||||
s.FileExists(filePath)
|
||||
|
||||
content, _ := os.ReadFile(filePath)
|
||||
s.Equal("test data", string(content))
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestReadFile() {
|
||||
func (s *SystemHelperTestSuite) TestRead() {
|
||||
filePath := "/tmp/testfile"
|
||||
defer os.Remove(filePath)
|
||||
|
||||
err := os.WriteFile(filePath, []byte("test data"), 0644)
|
||||
s.Nil(err)
|
||||
|
||||
s.Equal("test data", ReadFile(filePath))
|
||||
s.Equal("test data", Read(filePath))
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestRemoveFile() {
|
||||
func (s *SystemHelperTestSuite) TestRemove() {
|
||||
filePath := "/tmp/testfile"
|
||||
|
||||
err := os.WriteFile(filePath, []byte("test data"), 0644)
|
||||
s.Nil(err)
|
||||
|
||||
s.True(RemoveFile(filePath))
|
||||
s.True(Remove(filePath))
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestExecShell() {
|
||||
s.Equal("test", ExecShell("echo 'test'"))
|
||||
func (s *SystemHelperTestSuite) TestExec() {
|
||||
s.Equal("test", Exec("echo 'test'"))
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestExecShellAsync() {
|
||||
func (s *SystemHelperTestSuite) TestExecAsync() {
|
||||
command := "echo 'test' > /tmp/testfile"
|
||||
defer os.Remove("/tmp/testfile")
|
||||
|
||||
ExecShellAsync(command)
|
||||
ExecAsync(command)
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
@@ -105,12 +105,36 @@ func (s *SystemHelperTestSuite) TestEmpty() {
|
||||
s.False(Empty("/tmp"))
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestMv() {
|
||||
filePath := "/tmp/testfile"
|
||||
defer os.Remove(filePath)
|
||||
|
||||
err := os.WriteFile(filePath, []byte("test data"), 0644)
|
||||
s.Nil(err)
|
||||
|
||||
s.True(Mv(filePath, "/tmp/testfile2"))
|
||||
s.False(Exists(filePath))
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestCp() {
|
||||
filePath := "/tmp/testfile"
|
||||
defer os.Remove(filePath)
|
||||
|
||||
err := os.WriteFile(filePath, []byte("test data"), 0644)
|
||||
s.Nil(err)
|
||||
|
||||
s.True(Cp(filePath, "/tmp/testfile2"))
|
||||
s.True(Exists(filePath))
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestSize() {
|
||||
s.Equal(int64(0), Size("/tmp/123"))
|
||||
s.NotEqual(int64(0), Size("/tmp"))
|
||||
size, err := Size("/tmp/123")
|
||||
s.Equal(int64(0), size)
|
||||
s.Error(err)
|
||||
}
|
||||
|
||||
func (s *SystemHelperTestSuite) TestFileSize() {
|
||||
s.Equal(int64(0), FileSize("/tmp/123"))
|
||||
s.NotEqual(int64(0), FileSize("/tmp"))
|
||||
size, err := FileSize("/tmp/123")
|
||||
s.Equal(int64(0), size)
|
||||
s.Error(err)
|
||||
}
|
||||
|
||||
@@ -127,42 +127,42 @@ func UpdatePanel(proxy bool) error {
|
||||
color.Greenln("使用代理: " + strconv.FormatBool(proxy))
|
||||
|
||||
color.Greenln("备份面板配置...")
|
||||
ExecShell("cp -f /www/panel/database/panel.db /tmp/panel.db.bak")
|
||||
ExecShell("cp -f /www/panel/panel.conf /tmp/panel.conf.bak")
|
||||
Exec("cp -f /www/panel/database/panel.db /tmp/panel.db.bak")
|
||||
Exec("cp -f /www/panel/panel.conf /tmp/panel.conf.bak")
|
||||
if !Exists("/tmp/panel.db.bak") || !Exists("/tmp/panel.conf.bak") {
|
||||
return errors.New("备份面板配置失败")
|
||||
}
|
||||
color.Greenln("备份完成")
|
||||
|
||||
color.Greenln("清理旧版本...")
|
||||
ExecShell("rm -rf /www/panel/*")
|
||||
Exec("rm -rf /www/panel/*")
|
||||
color.Greenln("清理完成")
|
||||
|
||||
color.Greenln("正在下载...")
|
||||
if proxy {
|
||||
ExecShell("wget -O /www/panel/panel.zip https://ghproxy.com/" + panelInfo.DownloadUrl)
|
||||
Exec("wget -O /www/panel/panel.zip https://ghproxy.com/" + panelInfo.DownloadUrl)
|
||||
} else {
|
||||
ExecShell("wget -O /www/panel/panel.zip " + panelInfo.DownloadUrl)
|
||||
Exec("wget -O /www/panel/panel.zip " + panelInfo.DownloadUrl)
|
||||
}
|
||||
color.Greenln("下载完成")
|
||||
|
||||
color.Greenln("更新新版本...")
|
||||
ExecShell("cd /www/panel && unzip -o panel.zip && rm -rf panel.zip && chmod 700 panel && bash scripts/update_panel.sh")
|
||||
Exec("cd /www/panel && unzip -o panel.zip && rm -rf panel.zip && chmod 700 panel && bash scripts/update_panel.sh")
|
||||
color.Greenln("更新完成")
|
||||
|
||||
color.Greenln("恢复面板配置...")
|
||||
ExecShell("cp -f /tmp/panel.db.bak /www/panel/database/panel.db")
|
||||
ExecShell("cp -f /tmp/panel.conf.bak /www/panel/panel.conf")
|
||||
Exec("cp -f /tmp/panel.db.bak /www/panel/database/panel.db")
|
||||
Exec("cp -f /tmp/panel.conf.bak /www/panel/panel.conf")
|
||||
if !Exists("/www/panel/database/panel.db") || !Exists("/www/panel/panel.conf") {
|
||||
return errors.New("恢复面板配置失败")
|
||||
}
|
||||
ExecShell("/www/panel/panel --env=panel.conf artisan migrate")
|
||||
Exec("/www/panel/panel --env=panel.conf artisan migrate")
|
||||
color.Greenln("恢复完成")
|
||||
|
||||
ExecShell("panel writeSetting version " + panelInfo.Version)
|
||||
Exec("panel writeSetting version " + panelInfo.Version)
|
||||
|
||||
color.Greenln("重启面板...")
|
||||
ExecShell("systemctl restart panel")
|
||||
Exec("systemctl restart panel")
|
||||
color.Greenln("重启完成")
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user