Skip to content

Commit

Permalink
feat: 移除color包
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 25, 2024
1 parent bba5069 commit cfff459
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 134 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/go-resty/resty/v2 v2.15.3
github.com/go-sql-driver/mysql v1.8.1
github.com/goccy/go-yaml v1.12.0
github.com/gookit/color v1.5.4
github.com/gorilla/websocket v1.5.3
github.com/hashicorp/go-version v1.7.0
github.com/klauspost/compress v1.17.11
Expand Down Expand Up @@ -109,7 +108,6 @@ require (
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
Expand Down Expand Up @@ -310,8 +308,6 @@ github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli/v3 v3.0.0-alpha9.1 h1:1fJU+bltkwN8lF4Sni/X0i1d8XwPIrS82ivZ8qsp/q4=
github.com/urfave/cli/v3 v3.0.0-alpha9.1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
Expand Down
3 changes: 1 addition & 2 deletions internal/bootstrap/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"strings"

"github.com/gookit/color"
"github.com/urfave/cli/v3"

"github.com/TheTNB/panel/internal/app"
Expand Down Expand Up @@ -46,6 +45,6 @@ func initCli() {
Commands: route.Cli(),
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
color.Redln(fmt.Sprintf("|-%v", err))
fmt.Println(fmt.Sprintf("|-%v", err))
}
}
35 changes: 17 additions & 18 deletions internal/data/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/gookit/color"
"github.com/shirou/gopsutil/disk"

"github.com/TheTNB/panel/internal/app"
Expand Down Expand Up @@ -182,11 +181,11 @@ func (r *backupRepo) ClearExpired(path, prefix string, save int) error {
for _, file := range toDelete {
filePath := filepath.Join(path, file.Name())
if app.IsCli {
color.Greenln(fmt.Sprintf("|-清理过期文件:%s", filePath))
fmt.Println(fmt.Sprintf("|-清理过期文件:%s", filePath))

Check failure on line 184 in internal/data/backup.go

View workflow job for this annotation

GitHub Actions / golanci-lint

S1038: should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (gosimple)
}
if err = os.Remove(filePath); err != nil {
if app.IsCli {
color.Redln(fmt.Sprintf("|-清理失败:%v", err))
fmt.Println(fmt.Sprintf("|-清理失败:%v", err))

Check failure on line 188 in internal/data/backup.go

View workflow job for this annotation

GitHub Actions / golanci-lint

S1038: should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (gosimple)
} else {
return fmt.Errorf("清理失败:%v", err)
}
Expand Down Expand Up @@ -243,8 +242,8 @@ func (r *backupRepo) createWebsite(to string, name string) error {
}

if app.IsCli {
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup)))
fmt.Println(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))

Check failure on line 245 in internal/data/backup.go

View workflow job for this annotation

GitHub Actions / golanci-lint

S1038: should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (gosimple)
fmt.Println(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup)))
}
return nil
}
Expand Down Expand Up @@ -290,8 +289,8 @@ func (r *backupRepo) createMySQL(to string, name string) error {
}

if app.IsCli {
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup+".zip")))
fmt.Println(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
fmt.Println(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup+".zip")))
}
return nil
}
Expand Down Expand Up @@ -327,8 +326,8 @@ func (r *backupRepo) createPostgres(to string, name string) error {
}

if app.IsCli {
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup+".zip")))
fmt.Println(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
fmt.Println(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup+".zip")))
}
return nil
}
Expand All @@ -354,8 +353,8 @@ func (r *backupRepo) createPanel(to string) error {
}

if app.IsCli {
color.Greenln(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
color.Greenln(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup)))
fmt.Println(fmt.Sprintf("|-备份耗时:%s", time.Since(start).String()))
fmt.Println(fmt.Sprintf("|-已备份至文件:%s", filepath.Base(backup)))
}
return nil
}
Expand Down Expand Up @@ -478,10 +477,10 @@ func (r *backupRepo) preCheckPath(to, path string) error {
}

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))
fmt.Println(fmt.Sprintf("|-目标大小:%s", str.FormatBytes(float64(size))))
fmt.Println(fmt.Sprintf("|-目标文件数:%d", files))
fmt.Println(fmt.Sprintf("|-备份目录可用空间:%s", str.FormatBytes(float64(usage.Free))))
fmt.Println(fmt.Sprintf("|-备份目录可用Inode:%d", usage.InodesFree))
}

if uint64(size) > usage.Free {
Expand All @@ -504,9 +503,9 @@ func (r *backupRepo) preCheckDB(to string, size int64) error {
}

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))
fmt.Println(fmt.Sprintf("|-目标大小:%s", str.FormatBytes(float64(size))))
fmt.Println(fmt.Sprintf("|-备份目录可用空间:%s", str.FormatBytes(float64(usage.Free))))
fmt.Println(fmt.Sprintf("|-备份目录可用Inode:%d", usage.InodesFree))
}

if uint64(size) > usage.Free {
Expand Down
49 changes: 24 additions & 25 deletions internal/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/go-rat/utils/hash"
"github.com/goccy/go-yaml"
"github.com/gookit/color"
"github.com/spf13/cast"
"gorm.io/gorm"

Expand Down Expand Up @@ -243,13 +242,13 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {

name := filepath.Base(url)
if app.IsCli {
color.Greenln(fmt.Sprintf("|-目标版本:%s", version))
color.Greenln(fmt.Sprintf("|-下载链接:%s", url))
color.Greenln(fmt.Sprintf("|-文件名:%s", name))
fmt.Println(fmt.Sprintf("|-目标版本:%s", version))
fmt.Println(fmt.Sprintf("|-下载链接:%s", url))
fmt.Println(fmt.Sprintf("|-文件名:%s", name))
}

if app.IsCli {
color.Greenln("|-正在下载...")
fmt.Println("|-正在下载...")
}
if _, err := shell.Execf("wget -T 120 -t 3 -O /tmp/%s %s", name, url); err != nil {
return fmt.Errorf("下载失败:%w", err)
Expand All @@ -262,39 +261,39 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
}

if app.IsCli {
color.Greenln("|-校验下载文件...")
fmt.Println("|-校验下载文件...")
}
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 {
if app.IsCli {
color.Redln("|-清理校验文件失败:", err)
fmt.Println("|-清理校验文件失败:", err)
}
return fmt.Errorf("清理校验文件失败:%w", err)
}

if app.IsCli {
color.Greenln("|-前置检查...")
fmt.Println("|-前置检查...")
}
if io.Exists("/tmp/panel-storage.zip") {
return errors.New("检测到 /tmp 存在临时文件,可能是上次更新失败所致,请运行 panel-cli fix 修复后重试")
}

if app.IsCli {
color.Greenln("|-备份面板数据...")
fmt.Println("|-备份面板数据...")
}
// 备份面板
backup := NewBackupRepo()
if err := backup.Create(biz.BackupTypePanel, ""); err != nil {
if app.IsCli {
color.Redln("|-备份面板失败:", err)
fmt.Println("|-备份面板失败:", 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 {
if app.IsCli {
color.Redln("|-备份面板数据失败:", err)
fmt.Println("|-备份面板数据失败:", err)
}
return fmt.Errorf("备份面板数据失败:%w", err)
}
Expand All @@ -303,14 +302,14 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
}

if app.IsCli {
color.Greenln("|-清理旧版本...")
fmt.Println("|-清理旧版本...")
}
if _, err := shell.Execf("rm -rf %s/panel/*", app.Root); err != nil {
return fmt.Errorf("清理旧版本失败:%w", err)
}

if app.IsCli {
color.Greenln("|-解压新版本...")
fmt.Println("|-解压新版本...")
}
if err := io.UnCompress(filepath.Join("/tmp", name), filepath.Join(app.Root, "panel"), io.Zip); err != nil {
return fmt.Errorf("解压失败:%w", err)
Expand All @@ -320,7 +319,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
}

if app.IsCli {
color.Greenln("|-恢复面板数据...")
fmt.Println("|-恢复面板数据...")
}
if err := io.UnCompress("/tmp/panel-storage.zip", filepath.Join(app.Root, "panel"), io.Zip); err != nil {
return fmt.Errorf("恢复面板数据失败:%w", err)
Expand All @@ -330,7 +329,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
}

if app.IsCli {
color.Greenln("|-运行更新后脚本...")
fmt.Println("|-运行更新后脚本...")
}
if _, err := shell.Execf("curl -fsLm 10 https://dl.cdn.haozi.net/panel/auto_update.sh | bash"); err != nil {
return fmt.Errorf("运行面板更新后脚本失败:%w", err)
Expand All @@ -346,14 +345,14 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {
}

if app.IsCli {
color.Greenln("|-设置关键文件权限...")
fmt.Println("|-设置关键文件权限...")
}
_ = io.Chmod("/usr/local/sbin/panel-cli", 0700)
_ = io.Chmod("/etc/systemd/system/panel.service", 0700)
_ = io.Chmod(filepath.Join(app.Root, "panel"), 0700)

if app.IsCli {
color.Greenln("|-更新完成")
fmt.Println("|-更新完成")
}

_, _ = shell.Execf("systemctl daemon-reload")
Expand All @@ -366,7 +365,7 @@ func (r *settingRepo) UpdatePanel(version, url, checksum string) error {

func (r *settingRepo) FixPanel() error {
if app.IsCli {
color.Greenln("|-开始修复面板...")
fmt.Println("|-开始修复面板...")
}
// 检查关键文件是否正常
flag := false
Expand All @@ -393,7 +392,7 @@ func (r *settingRepo) FixPanel() error {
return fmt.Errorf("清理临时文件失败:%w", err)
}
if app.IsCli {
color.Greenln("已清理临时文件,请运行 panel-cli update 更新面板")
fmt.Println("已清理临时文件,请运行 panel-cli update 更新面板")
}
return nil
}
Expand All @@ -413,12 +412,12 @@ func (r *settingRepo) FixPanel() error {
}
latest := list[0]
if app.IsCli {
color.Greenln(fmt.Sprintf("|-使用备份文件:%s", latest.Name))
fmt.Println(fmt.Sprintf("|-使用备份文件:%s", latest.Name))
}

// 解压备份文件
if app.IsCli {
color.Greenln("|-解压备份文件...")
fmt.Println("|-解压备份文件...")
}
if err = io.Remove("/tmp/panel-fix"); err != nil {
return fmt.Errorf("清理临时目录失败:%w", err)
Expand All @@ -429,7 +428,7 @@ func (r *settingRepo) FixPanel() error {

// 移动文件到对应位置
if app.IsCli {
color.Greenln("|-移动备份文件...")
fmt.Println("|-移动备份文件...")
}
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 {
Expand All @@ -452,7 +451,7 @@ func (r *settingRepo) FixPanel() error {

// tmp目录下如果有storage备份,则解压回去
if app.IsCli {
color.Greenln("|-恢复面板数据...")
fmt.Println("|-恢复面板数据...")
}
if io.Exists("/tmp/panel-storage.zip") {
if err = io.UnCompress("/tmp/panel-storage.zip", filepath.Join(app.Root, "panel"), io.Zip); err != nil {
Expand All @@ -472,7 +471,7 @@ func (r *settingRepo) FixPanel() error {

// 处理权限
if app.IsCli {
color.Greenln("|-设置关键文件权限...")
fmt.Println("|-设置关键文件权限...")
}
if err = io.Chmod("/usr/local/etc/panel/config.yml", 0600); err != nil {
return err
Expand All @@ -488,7 +487,7 @@ func (r *settingRepo) FixPanel() error {
}

if app.IsCli {
color.Greenln("|-修复完成")
fmt.Println("|-修复完成")
}

tools.RestartPanel()
Expand Down
Loading

0 comments on commit cfff459

Please sign in to comment.