mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 06:47:20 +08:00
feat: 仅在cli中打印信息
This commit is contained in:
@@ -30,4 +30,5 @@ var (
|
||||
Root string
|
||||
Version string
|
||||
Locale string
|
||||
IsCli bool
|
||||
)
|
||||
|
||||
@@ -14,6 +14,8 @@ import (
|
||||
)
|
||||
|
||||
func initCli() {
|
||||
app.IsCli = true
|
||||
|
||||
cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "NAME", "名称")
|
||||
cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "USAGE", "用法")
|
||||
cli.RootCommandHelpTemplate = strings.ReplaceAll(cli.RootCommandHelpTemplate, "VERSION", "版本")
|
||||
|
||||
@@ -181,9 +181,15 @@ func (r *backupRepo) ClearExpired(path, prefix string, save int) error {
|
||||
toDelete := filtered[save:]
|
||||
for _, file := range toDelete {
|
||||
filePath := filepath.Join(path, file.Name())
|
||||
color.Greenln(fmt.Sprintf("|-清理过期文件:%s", filePath))
|
||||
if app.IsCli {
|
||||
color.Greenln(fmt.Sprintf("|-清理过期文件:%s", filePath))
|
||||
}
|
||||
if err = os.Remove(filePath); err != nil {
|
||||
color.Redln(fmt.Sprintf("|-清理失败:%v", err))
|
||||
if app.IsCli {
|
||||
color.Redln(fmt.Sprintf("|-清理失败:%v", err))
|
||||
} else {
|
||||
return fmt.Errorf("清理失败:%v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,8 +242,10 @@ func (r *backupRepo) createWebsite(to string, name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
|
||||
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup)))
|
||||
if app.IsCli {
|
||||
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
|
||||
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -281,8 +289,10 @@ func (r *backupRepo) createMySQL(to string, name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
|
||||
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup+".zip")))
|
||||
if app.IsCli {
|
||||
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
|
||||
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup+".zip")))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -316,8 +326,10 @@ func (r *backupRepo) createPostgres(to string, name string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
|
||||
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup+".zip")))
|
||||
if app.IsCli {
|
||||
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
|
||||
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup+".zip")))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -341,8 +353,10 @@ func (r *backupRepo) createPanel(to string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
|
||||
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup)))
|
||||
if app.IsCli {
|
||||
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
|
||||
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -463,10 +477,12 @@ func (r *backupRepo) preCheckPath(to, path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
color.Greenln(fmt.Sprintf("|-目标大小:%s", str.FormatBytes(float64(size))))
|
||||
color.Greenln(fmt.Sprintf("|-目标文件数:%d", files))
|
||||
color.Greenln(fmt.Sprintf("|-备份目录可用空间:%s", str.FormatBytes(float64(usage.Free))))
|
||||
color.Greenln(fmt.Sprintf("|-备份目录可用Inode:%d", usage.InodesFree))
|
||||
if app.IsCli {
|
||||
color.Greenln(fmt.Sprintf("|-目标大小:%s", str.FormatBytes(float64(size))))
|
||||
color.Greenln(fmt.Sprintf("|-目标文件数:%d", files))
|
||||
color.Greenln(fmt.Sprintf("|-备份目录可用空间:%s", str.FormatBytes(float64(usage.Free))))
|
||||
color.Greenln(fmt.Sprintf("|-备份目录可用Inode:%d", usage.InodesFree))
|
||||
}
|
||||
|
||||
if uint64(size) > usage.Free {
|
||||
return errors.New("备份目录空间不足")
|
||||
@@ -487,9 +503,11 @@ func (r *backupRepo) preCheckDB(to string, size int64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
color.Greenln(fmt.Sprintf("|-目标大小:%s", str.FormatBytes(float64(size))))
|
||||
color.Greenln(fmt.Sprintf("|-备份目录可用空间:%s", str.FormatBytes(float64(usage.Free))))
|
||||
color.Greenln(fmt.Sprintf("|-备份目录可用Inode:%d", usage.InodesFree))
|
||||
if app.IsCli {
|
||||
color.Greenln(fmt.Sprintf("|-目标大小:%s", str.FormatBytes(float64(size))))
|
||||
color.Greenln(fmt.Sprintf("|-备份目录可用空间:%s", str.FormatBytes(float64(usage.Free))))
|
||||
color.Greenln(fmt.Sprintf("|-备份目录可用Inode:%d", usage.InodesFree))
|
||||
}
|
||||
|
||||
if uint64(size) > usage.Free {
|
||||
return errors.New("备份目录空间不足")
|
||||
|
||||
@@ -186,11 +186,15 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
|
||||
}
|
||||
|
||||
name := filepath.Base(url)
|
||||
color.Greenln(fmt.Sprintf("|-目标版本:%s", version))
|
||||
color.Greenln(fmt.Sprintf("|-下载链接:%s", url))
|
||||
color.Greenln(fmt.Sprintf("|-文件名:%s", name))
|
||||
if app.IsCli {
|
||||
color.Greenln(fmt.Sprintf("|-目标版本:%s", version))
|
||||
color.Greenln(fmt.Sprintf("|-下载链接:%s", url))
|
||||
color.Greenln(fmt.Sprintf("|-文件名:%s", name))
|
||||
}
|
||||
|
||||
color.Greenln("|-正在下载...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-正在下载...")
|
||||
}
|
||||
if _, err := shell.Execf("wget -T 120 -t 3 -O /tmp/%s %s", name, url); err != nil {
|
||||
return fmt.Errorf("下载失败:%w", err)
|
||||
}
|
||||
@@ -201,41 +205,57 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
|
||||
return errors.New("下载文件检查失败")
|
||||
}
|
||||
|
||||
color.Greenln("|-校验下载文件...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-校验下载文件...")
|
||||
}
|
||||
if check, err := shell.Execf("cd /tmp && sha256sum -c %s --ignore-missing", name+".sha256"); check != name+": OK" || err != nil {
|
||||
return errors.New("下载文件校验失败")
|
||||
}
|
||||
if err := io.Remove(filepath.Join("/tmp", name+".sha256")); err != nil {
|
||||
color.Redln("|-清理校验文件失败:", err)
|
||||
return err
|
||||
if app.IsCli {
|
||||
color.Redln("|-清理校验文件失败:", err)
|
||||
}
|
||||
return fmt.Errorf("清理校验文件失败:%w", err)
|
||||
}
|
||||
|
||||
color.Greenln("|-前置检查...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-前置检查...")
|
||||
}
|
||||
if io.Exists("/tmp/panel-storage.zip") {
|
||||
return errors.New("检测到 /tmp 存在临时文件,可能是上次更新失败所致,请运行 panel-cli fix 修复后重试")
|
||||
}
|
||||
|
||||
color.Greenln("|-备份面板数据...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-备份面板数据...")
|
||||
}
|
||||
// 备份面板
|
||||
backup := NewBackupRepo()
|
||||
if err := backup.Create(biz.BackupTypePanel, ""); err != nil {
|
||||
color.Redln("|-备份面板失败:", err)
|
||||
return err
|
||||
if app.IsCli {
|
||||
color.Redln("|-备份面板失败:", err)
|
||||
}
|
||||
return fmt.Errorf("备份面板失败:%w", err)
|
||||
}
|
||||
if err := io.Compress([]string{filepath.Join(app.Root, "panel/storage")}, "/tmp/panel-storage.zip", io.Zip); err != nil {
|
||||
color.Redln("|-备份面板数据失败:", err)
|
||||
return err
|
||||
if app.IsCli {
|
||||
color.Redln("|-备份面板数据失败:", err)
|
||||
}
|
||||
return fmt.Errorf("备份面板数据失败:%w", err)
|
||||
}
|
||||
if !io.Exists("/tmp/panel-storage.zip") {
|
||||
return errors.New("已备份面板数据检查失败")
|
||||
}
|
||||
|
||||
color.Greenln("|-清理旧版本...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-清理旧版本...")
|
||||
}
|
||||
if _, err := shell.Execf("rm -rf %s/panel/*", app.Root); err != nil {
|
||||
return fmt.Errorf("清理旧版本失败:%w", err)
|
||||
}
|
||||
|
||||
color.Greenln("|-解压新版本...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-解压新版本...")
|
||||
}
|
||||
if err := io.UnCompress(filepath.Join("/tmp", name), filepath.Join(app.Root, "panel"), io.Zip); err != nil {
|
||||
return fmt.Errorf("解压失败:%w", err)
|
||||
}
|
||||
@@ -243,7 +263,9 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
|
||||
return errors.New("解压失败,缺失文件")
|
||||
}
|
||||
|
||||
color.Greenln("|-恢复面板数据...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-恢复面板数据...")
|
||||
}
|
||||
if err := io.UnCompress("/tmp/panel-storage.zip", filepath.Join(app.Root, "panel"), io.Zip); err != nil {
|
||||
return fmt.Errorf("恢复面板数据失败:%w", err)
|
||||
}
|
||||
@@ -251,7 +273,9 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
|
||||
return errors.New("恢复面板数据失败")
|
||||
}
|
||||
|
||||
color.Greenln("|-运行升级后脚本...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-运行升级后脚本...")
|
||||
}
|
||||
if _, err := shell.Execf("curl -fsLm 10 https://dl.cdn.haozi.net/panel/auto_update.sh | bash"); err != nil {
|
||||
return fmt.Errorf("运行面板升级后脚本失败:%w", err)
|
||||
}
|
||||
@@ -265,12 +289,16 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
|
||||
return fmt.Errorf("移动面板命令行工具失败:%w", err)
|
||||
}
|
||||
|
||||
color.Greenln("|-设置关键文件权限...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-设置关键文件权限...")
|
||||
}
|
||||
_ = io.Chmod("/usr/local/sbin/panel-cli", 0700)
|
||||
_ = io.Chmod("/etc/systemd/system/panel.service", 0700)
|
||||
_ = io.Chmod(filepath.Join(app.Root, "panel"), 0700)
|
||||
|
||||
color.Greenln("|-升级完成")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-升级完成")
|
||||
}
|
||||
|
||||
_, _ = shell.Execf("systemctl daemon-reload")
|
||||
_ = io.Remove("/tmp/panel-storage.zip")
|
||||
@@ -281,7 +309,9 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
|
||||
}
|
||||
|
||||
func (r *settingRepo) FixPanel() error {
|
||||
color.Greenln("|-开始修复面板...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-开始修复面板...")
|
||||
}
|
||||
// 检查关键文件是否正常
|
||||
flag := false
|
||||
if !io.Exists(filepath.Join(app.Root, "panel", "web")) {
|
||||
@@ -306,7 +336,9 @@ func (r *settingRepo) FixPanel() error {
|
||||
if err := io.Remove("/tmp/panel-storage.zip"); err != nil {
|
||||
return fmt.Errorf("清理临时文件失败:%w", err)
|
||||
}
|
||||
color.Greenln("已清理临时文件,请运行 panel-cli update 升级面板")
|
||||
if app.IsCli {
|
||||
color.Greenln("已清理临时文件,请运行 panel-cli update 升级面板")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -324,10 +356,14 @@ func (r *settingRepo) FixPanel() error {
|
||||
return fmt.Errorf("未找到备份文件,无法自动修复")
|
||||
}
|
||||
latest := list[0]
|
||||
color.Greenln(fmt.Sprintf("|-使用备份文件:%s", latest.Name))
|
||||
if app.IsCli {
|
||||
color.Greenln(fmt.Sprintf("|-使用备份文件:%s", latest.Name))
|
||||
}
|
||||
|
||||
// 解压备份文件
|
||||
color.Greenln("|-解压备份文件...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-解压备份文件...")
|
||||
}
|
||||
if err = io.Remove("/tmp/panel-fix"); err != nil {
|
||||
return fmt.Errorf("清理临时目录失败:%w", err)
|
||||
}
|
||||
@@ -336,7 +372,9 @@ func (r *settingRepo) FixPanel() error {
|
||||
}
|
||||
|
||||
// 移动文件到对应位置
|
||||
color.Greenln("|-移动备份文件...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-移动备份文件...")
|
||||
}
|
||||
if io.Exists(filepath.Join("/tmp/panel-fix", "panel")) && io.IsDir(filepath.Join("/tmp/panel-fix", "panel")) {
|
||||
if err = io.Remove(filepath.Join(app.Root, "panel")); err != nil {
|
||||
return fmt.Errorf("删除目录失败:%w", err)
|
||||
@@ -357,7 +395,9 @@ func (r *settingRepo) FixPanel() error {
|
||||
}
|
||||
|
||||
// tmp目录下如果有storage备份,则解压回去
|
||||
color.Greenln("|-恢复面板数据...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-恢复面板数据...")
|
||||
}
|
||||
if io.Exists("/tmp/panel-storage.zip") {
|
||||
if err = io.UnCompress("/tmp/panel-storage.zip", filepath.Join(app.Root, "panel"), io.Zip); err != nil {
|
||||
return fmt.Errorf("恢复面板数据失败:%w", err)
|
||||
@@ -375,7 +415,9 @@ func (r *settingRepo) FixPanel() error {
|
||||
}
|
||||
|
||||
// 处理权限
|
||||
color.Greenln("|-设置关键文件权限...")
|
||||
if app.IsCli {
|
||||
color.Greenln("|-设置关键文件权限...")
|
||||
}
|
||||
if err = io.Chmod("/usr/local/etc/panel/config.yml", 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -389,8 +431,10 @@ func (r *settingRepo) FixPanel() error {
|
||||
return err
|
||||
}
|
||||
|
||||
color.Greenln("|-修复完成")
|
||||
tools.RestartPanel()
|
||||
if app.IsCli {
|
||||
color.Greenln("|-修复完成")
|
||||
}
|
||||
|
||||
tools.RestartPanel()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user