From e8ece0dfd7928a940b9469897beb183fd7ecf22f Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:03:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=84=9A=E6=9C=AC=E6=89=A7=E8=A1=8C=E8=BE=93?= =?UTF-8?q?=E5=87=BA=20(#2637)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/i18n/lang/en.yaml | 2 +- backend/i18n/lang/zh-Hant.yaml | 2 +- backend/i18n/lang/zh.yaml | 2 +- backend/utils/cmd/cmd.go | 16 ++++++---------- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml index 97e5a6a0a..ff0e294b9 100644 --- a/backend/i18n/lang/en.yaml +++ b/backend/i18n/lang/en.yaml @@ -131,4 +131,4 @@ ErrUserFindErr: "Failed to find user {{ .name }} {{ .err }}" ErrFirewall: "No firewalld or ufw service is detected. Please check and try again!" #cronjob -ErrBashExecute: "Script execution error, please check stderr-related information in the task output text field." \ No newline at end of file +ErrBashExecute: "Script execution error, please check the specific information in the task output text area." \ No newline at end of file diff --git a/backend/i18n/lang/zh-Hant.yaml b/backend/i18n/lang/zh-Hant.yaml index e73fae535..9ce558c2a 100644 --- a/backend/i18n/lang/zh-Hant.yaml +++ b/backend/i18n/lang/zh-Hant.yaml @@ -131,4 +131,4 @@ ErrUserFindErr: "用戶 {{ .name }} 查找失敗 {{ .err }}" ErrFirewall: "當前未檢測到系統 firewalld 或 ufw 服務,請檢查後重試!" #cronjob -ErrBashExecute: "腳本執行錯誤,請在任務輸出文本區域中查看 stderr 相關信息。" \ No newline at end of file +ErrBashExecute: "腳本執行錯誤,請在任務輸出文本域中查看具體信息。" \ No newline at end of file diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml index f2ccf246d..c6a492dcc 100644 --- a/backend/i18n/lang/zh.yaml +++ b/backend/i18n/lang/zh.yaml @@ -131,4 +131,4 @@ ErrUserFindErr: "用户 {{ .name }} 查找失败 {{ .err }}" ErrFirewall: "当前未检测到系统 firewalld 或 ufw 服务,请检查后重试!" #cronjob -ErrBashExecute: "脚本执行错误,请在任务输出文本域中查看 stderr 相关信息。" +ErrBashExecute: "脚本执行错误,请在任务输出文本域中查看具体信息。" diff --git a/backend/utils/cmd/cmd.go b/backend/utils/cmd/cmd.go index deea87535..409bf9f65 100644 --- a/backend/utils/cmd/cmd.go +++ b/backend/utils/cmd/cmd.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "io" "os/exec" "strings" "time" @@ -81,24 +82,19 @@ func ExecCronjobWithTimeOut(cmdStr string, workdir string, timeout time.Duration var stdout, stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr + output := new(bytes.Buffer) + cmd.Stdout = io.MultiWriter(output, cmd.Stdout) + cmd.Stderr = io.MultiWriter(output, cmd.Stderr) + err := cmd.Run() if ctx.Err() == context.DeadlineExceeded { return "", buserr.New(constant.ErrCmdTimeout) } - errMsg := "" if len(stderr.String()) != 0 { - errMsg = fmt.Sprintf("stderr:\n%s", stderr.String()) err = buserr.New(constant.ErrBashExecute) } - if len(stdout.String()) != 0 { - if len(errMsg) != 0 { - errMsg = fmt.Sprintf("%s \n\n stdout:\n%s", errMsg, stdout.String()) - } else { - errMsg = fmt.Sprintf("stdout:\n %s", stdout.String()) - } - } - return errMsg, err + return output.String(), err } func Execf(cmdStr string, a ...interface{}) (string, error) {