From 7cb497929b49fe537448fe2f639a61045092ae01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Thu, 17 Oct 2024 12:39:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20cli=E4=B8=8B=E5=AE=89=E8=A3=85=E5=BA=94?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/data/app.go | 28 ++++++++++++++++------------ pkg/shell/exec.go | 10 ++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/internal/data/app.go b/internal/data/app.go index cb175a03..b6eba8b0 100644 --- a/internal/data/app.go +++ b/internal/data/app.go @@ -14,6 +14,7 @@ import ( "github.com/TheTNB/panel/internal/biz" "github.com/TheTNB/panel/pkg/api" "github.com/TheTNB/panel/pkg/apploader" + "github.com/TheTNB/panel/pkg/shell" "github.com/TheTNB/panel/pkg/str" ) @@ -179,16 +180,17 @@ func (r *appRepo) Install(channel, slug string) error { return err } + if app.IsCli { + return shell.ExecfWithOutput(`curl -fsLm 10 --retry 3 "%s" | bash -s -- "%s" "%s"`, shellUrl, shellChannel, shellVersion) + } + task := new(biz.Task) task.Name = "安装应用 " + item.Name task.Status = biz.TaskStatusWaiting task.Shell = fmt.Sprintf(`curl -fsLm 10 --retry 3 "%s" | bash -s -- "%s" "%s" >> /tmp/%s.log 2>&1`, shellUrl, shellChannel, shellVersion, item.Slug) task.Log = "/tmp/" + item.Slug + ".log" - if err = r.taskRepo.Push(task); err != nil { - return err - } - return err + return r.taskRepo.Push(task) } func (r *appRepo) UnInstall(slug string) error { @@ -233,16 +235,17 @@ func (r *appRepo) UnInstall(slug string) error { return err } + if app.IsCli { + return shell.ExecfWithOutput(`curl -fsLm 10 --retry 3 "%s" | bash -s -- "%s" "%s"`, shellUrl, shellChannel, shellVersion) + } + task := new(biz.Task) task.Name = "卸载应用 " + item.Name task.Status = biz.TaskStatusWaiting task.Shell = fmt.Sprintf(`curl -fsLm 10 --retry 3 "%s" | bash -s -- "%s" "%s" >> /tmp/%s.log 2>&1`, shellUrl, shellChannel, shellVersion, item.Slug) task.Log = "/tmp/" + item.Slug + ".log" - if err = r.taskRepo.Push(task); err != nil { - return err - } - return err + return r.taskRepo.Push(task) } func (r *appRepo) Update(slug string) error { @@ -287,16 +290,17 @@ func (r *appRepo) Update(slug string) error { return err } + if app.IsCli { + return shell.ExecfWithOutput(`curl -fsLm 10 --retry 3 "%s" | bash -s -- "%s" "%s"`, shellUrl, shellChannel, shellVersion) + } + task := new(biz.Task) task.Name = "升级应用 " + item.Name task.Status = biz.TaskStatusWaiting task.Shell = fmt.Sprintf(`curl -fsLm 10 --retry 3 "%s" | bash -s -- "%s" "%s" >> /tmp/%s.log 2>&1`, shellUrl, shellChannel, shellVersion, item.Slug) task.Log = "/tmp/" + item.Slug + ".log" - if err = r.taskRepo.Push(task); err != nil { - return err - } - return err + return r.taskRepo.Push(task) } func (r *appRepo) UpdateShow(slug string, show bool) error { diff --git a/pkg/shell/exec.go b/pkg/shell/exec.go index 3263def2..013d5482 100644 --- a/pkg/shell/exec.go +++ b/pkg/shell/exec.go @@ -80,3 +80,13 @@ func ExecfWithTimeout(timeout time.Duration, shell string, args ...any) (string, return strings.TrimSpace(stdout.String()), err } + +// ExecfWithOutput 执行 shell 命令并输出到终端 +func ExecfWithOutput(shell string, args ...any) error { + _ = os.Setenv("LC_ALL", "C") + cmd := exec.Command("bash", "-c", fmt.Sprintf(shell, args...)) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd.Run() +}