2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 20:57:19 +08:00

refactor: 重构 tools.Remove 函数

This commit is contained in:
耗子
2023-11-14 02:16:18 +08:00
parent 83cbae034c
commit f7d65fc22e
14 changed files with 80 additions and 35 deletions

View File

@@ -290,7 +290,10 @@ func (receiver *Panel) Handle(ctx console.Context) error {
color.Redln("|-压缩失败: " + err.Error())
return nil
}
tools.Remove("/tmp/" + backupFile)
if err := tools.Remove("/tmp/" + backupFile); err != nil {
color.Redln("|-删除失败: " + err.Error())
return nil
}
color.Greenln("|-压缩成功")
color.Greenln("|-开始移动")
if err := tools.Mv("/tmp/"+backupFile+".zip", path+"/"+backupFile+".zip"); err != nil {
@@ -327,7 +330,10 @@ func (receiver *Panel) Handle(ctx console.Context) error {
color.Redln("|-压缩失败: " + err.Error())
return nil
}
tools.Remove("/tmp/" + backupFile)
if err := tools.Remove("/tmp/" + backupFile); err != nil {
color.Redln("|-删除失败: " + err.Error())
return nil
}
color.Greenln("|-压缩成功")
color.Greenln("|-开始移动")
if err := tools.Mv("/tmp/"+backupFile+".zip", path+"/"+backupFile+".zip"); err != nil {
@@ -360,7 +366,10 @@ func (receiver *Panel) Handle(ctx console.Context) error {
for i := cast.ToInt(save); i < len(filteredFiles); i++ {
fileToDelete := filepath.Join(path, filteredFiles[i].Name())
color.Yellowln("|-清理备份: " + fileToDelete)
tools.Remove(fileToDelete)
if err := tools.Remove(fileToDelete); err != nil {
color.Redln("|-清理失败: " + err.Error())
return nil
}
}
color.Greenln("|-清理完成")
color.Greenln(hr)
@@ -428,7 +437,10 @@ func (receiver *Panel) Handle(ctx console.Context) error {
for i := cast.ToInt(save); i < len(filteredFiles); i++ {
fileToDelete := filepath.Join("/www/wwwlogs", filteredFiles[i].Name())
color.Yellowln("|-清理日志: " + fileToDelete)
tools.Remove(fileToDelete)
if err := tools.Remove(fileToDelete); err != nil {
color.Redln("|-清理失败: " + err.Error())
return nil
}
}
color.Greenln("|-清理完成")
color.Greenln(hr)

View File

@@ -235,7 +235,9 @@ func (r *CronController) Delete(ctx http.Context) http.Response {
if err := r.cron.DeleteFromSystem(cron); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
tools.Remove(cron.Shell)
if err := tools.Remove(cron.Shell); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
if _, err := facades.Orm().Query().Delete(&cron); err != nil {
facades.Log().Request(ctx.Request()).Tags("面板", "计划任务").With(map[string]any{

View File

@@ -556,7 +556,9 @@ func (r *Mysql57Controller) DeleteBackup(ctx http.Context) http.Response {
backupPath := r.setting.Get(models.SettingKeyBackupPath) + "/mysql"
fileName := ctx.Request().Input("name")
tools.Remove(backupPath + "/" + fileName)
if err := tools.Remove(backupPath + "/" + fileName); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
return controllers.Success(ctx, nil)
}

View File

@@ -556,7 +556,9 @@ func (r *Mysql80Controller) DeleteBackup(ctx http.Context) http.Response {
backupPath := r.setting.Get(models.SettingKeyBackupPath) + "/mysql"
fileName := ctx.Request().Input("name")
tools.Remove(backupPath + "/" + fileName)
if err := tools.Remove(backupPath + "/" + fileName); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
return controllers.Success(ctx, nil)
}

View File

@@ -489,7 +489,9 @@ func (r *Postgresql15Controller) DeleteBackup(ctx http.Context) http.Response {
backupPath := r.setting.Get(models.SettingKeyBackupPath) + "/postgresql"
fileName := ctx.Request().Input("name")
tools.Remove(backupPath + "/" + fileName)
if err := tools.Remove(backupPath + "/" + fileName); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
return controllers.Success(ctx, nil)
}

View File

@@ -489,7 +489,9 @@ func (r *Postgresql16Controller) DeleteBackup(ctx http.Context) http.Response {
backupPath := r.setting.Get(models.SettingKeyBackupPath) + "/postgresql"
fileName := ctx.Request().Input("name")
tools.Remove(backupPath + "/" + fileName)
if err := tools.Remove(backupPath + "/" + fileName); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
return controllers.Success(ctx, nil)
}

View File

@@ -192,7 +192,9 @@ func (r *S3fsController) Delete(ctx http.Context) http.Response {
if mountCheck, err := tools.Exec("mount -a 2>&1"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "检测到/etc/fstab有误: "+mountCheck)
}
tools.Remove("/etc/passwd-s3fs-" + cast.ToString(mount.ID))
if err := tools.Remove("/etc/passwd-s3fs-" + cast.ToString(mount.ID)); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
var newS3fsList []S3fsMount
for _, s := range s3fsList {

View File

@@ -471,17 +471,23 @@ func (r *SupervisorController) DeleteProcess(ctx http.Context) http.Response {
var err error
if tools.IsRHEL() {
logPath, err = tools.Exec(`cat '/etc/supervisord.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`)
tools.Remove(`/etc/supervisord.d/` + process + `.conf`)
if err := tools.Remove(`/etc/supervisord.d/` + process + `.conf`); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
} else {
logPath, err = tools.Exec(`cat '/etc/supervisor/conf.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`)
tools.Remove(`/etc/supervisor/conf.d/` + process + `.conf`)
if err := tools.Remove(`/etc/supervisor/conf.d/` + process + `.conf`); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
}
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "无法从进程"+process+"的配置文件中获取日志路径")
}
tools.Remove(logPath)
if err := tools.Remove(logPath); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
if out, err := tools.Exec(`supervisorctl reread`); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, out)
}

View File

@@ -219,7 +219,9 @@ func (r *ToolBoxController) SetTimezone(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusUnprocessableEntity, "时区不能为空")
}
tools.Exec("timedatectl set-timezone " + timezone)
if out, err := tools.Exec("timedatectl set-timezone " + timezone); err != nil {
return controllers.Error(ctx, http.StatusUnprocessableEntity, out)
}
return controllers.Success(ctx, nil)
}
@@ -274,7 +276,9 @@ func (r *ToolBoxController) SetRootPassword(ctx http.Context) http.Response {
}
password = strings.ReplaceAll(password, `'`, `\'`)
tools.Exec(`yes '` + password + `' | passwd root`)
if out, err := tools.Exec(`yes '` + password + `' | passwd root`); err != nil {
return controllers.Error(ctx, http.StatusUnprocessableEntity, out)
}
return controllers.Success(ctx, nil)
}

View File

@@ -312,7 +312,10 @@ func (r *WebsiteController) ClearLog(ctx http.Context) http.Response {
return ErrorSystem(ctx)
}
tools.Remove("/www/wwwlogs/" + website.Name + ".log")
if err := tools.Remove("/www/wwwlogs/" + website.Name + ".log"); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
return Success(ctx, nil)
}
@@ -507,8 +510,8 @@ func (r *WebsiteController) DeleteBackup(ctx http.Context) http.Response {
}
}
if !tools.Remove(backupPath + "/" + deleteBackupRequest.Name) {
return Error(ctx, http.StatusInternalServerError, "删除备份失败")
if err := tools.Remove(backupPath + "/" + deleteBackupRequest.Name); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
return Success(ctx, nil)

View File

@@ -184,7 +184,9 @@ func (s *BackupImpl) MysqlBackup(database string) error {
if _, err := tools.Exec("cd " + backupPath + " && zip -r " + backupPath + "/" + backupFile + ".zip " + backupFile); err != nil {
return err
}
tools.Remove(backupPath + "/" + backupFile)
if err := tools.Remove(backupPath + "/" + backupFile); err != nil {
return err
}
return os.Unsetenv("MYSQL_PWD")
}
@@ -302,8 +304,7 @@ func (s *BackupImpl) PostgresqlBackup(database string) error {
return err
}
tools.Remove(backupPath + "/" + backupFile)
return nil
return tools.Remove(backupPath + "/" + backupFile)
}
// PostgresqlRestore PostgreSQL恢复

View File

@@ -420,7 +420,9 @@ func (r *WebsiteImpl) SaveConfig(config requests.SaveConfig) error {
}
} else {
if tools.Exists(root + ".user.ini") {
tools.Remove(root + ".user.ini")
if err := tools.Remove(root + ".user.ini"); err != nil {
return err
}
}
}
@@ -534,11 +536,21 @@ func (r *WebsiteImpl) Delete(id uint) error {
return err
}
tools.Remove("/www/server/vhost/" + website.Name + ".conf")
tools.Remove("/www/server/vhost/rewrite/" + website.Name + ".conf")
tools.Remove("/www/server/vhost/ssl/" + website.Name + ".pem")
tools.Remove("/www/server/vhost/ssl/" + website.Name + ".key")
tools.Remove(website.Path)
if err := tools.Remove("/www/server/vhost/" + website.Name + ".conf"); err != nil {
return err
}
if err := tools.Remove("/www/server/vhost/rewrite/" + website.Name + ".conf"); err != nil {
return err
}
if err := tools.Remove("/www/server/vhost/ssl/" + website.Name + ".pem"); err != nil {
return err
}
if err := tools.Remove("/www/server/vhost/ssl/" + website.Name + ".key"); err != nil {
return err
}
if err := tools.Remove(website.Path); err != nil {
return err
}
_, err := tools.Exec("systemctl reload openresty")
return err

View File

@@ -31,13 +31,8 @@ func Read(path string) (string, error) {
}
// Remove 删除文件/目录
// TODO 重构带 error 返回
func Remove(path string) bool {
if err := os.RemoveAll(path); err != nil {
return false
}
return true
func Remove(path string) error {
return os.RemoveAll(path)
}
// Exec 执行 shell 命令

View File

@@ -46,7 +46,7 @@ func (s *SystemHelperTestSuite) TestRemove() {
err := os.WriteFile(filePath, []byte("test data"), 0644)
s.Nil(err)
s.True(Remove(filePath))
s.Nil(Remove(filePath))
}
func (s *SystemHelperTestSuite) TestExec() {