From c768a8150710628357cbc4552906ba7ac42a2328 Mon Sep 17 00:00:00 2001 From: yangyile Date: Mon, 16 Dec 2024 17:55:38 +0700 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8done=E5=8C=85=E6=8A=8A?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E8=BF=94=E5=9B=9E=E5=80=BC=E5=90=88=E8=B5=B7?= =?UTF-8?q?=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command.go | 8 ++++---- go.mod | 2 +- internal/utils/utils.go | 19 ++++++++++--------- osexec.go | 13 +++++-------- sure.gen.go | 15 +++++++++++++++ 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/command.go b/command.go index 94b5f96..daf1779 100644 --- a/command.go +++ b/command.go @@ -9,6 +9,7 @@ import ( "strings" "sync" + "github.com/yyle88/done" "github.com/yyle88/erero" "github.com/yyle88/eroticgo" "github.com/yyle88/osexec/internal/utils" @@ -104,8 +105,7 @@ func (c *CommandConfig) Exec(name string, args ...string) ([]byte, error) { return nil, erero.Ero(err) } command := c.prepareCommand(name, args) - output, err := command.CombinedOutput() - return utils.WarpMessage(output, err, c.DebugMode) + return utils.WarpMessage(done.VAE(command.CombinedOutput()), c.DebugMode) } func (c *CommandConfig) validateConfig(name string, args []string) error { @@ -203,9 +203,9 @@ func (c *CommandConfig) StreamExec(name string, args ...string) ([]byte, error) wg.Wait() if stderrBuffer.Len() > 0 { - return utils.WarpMessage(stdoutBuffer.Bytes(), erero.New(stderrBuffer.String()), c.DebugMode) + return utils.WarpMessage(done.VAE(stdoutBuffer.Bytes(), erero.New(stderrBuffer.String())), c.DebugMode) } else { - return utils.WarpMessage(stdoutBuffer.Bytes(), nil, c.DebugMode) + return utils.WarpMessage(done.VAE(stdoutBuffer.Bytes(), nil), c.DebugMode) } } diff --git a/go.mod b/go.mod index 30a6dcc..c1e994f 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.22.8 require ( github.com/stretchr/testify v1.10.0 + github.com/yyle88/done v1.0.18 github.com/yyle88/erero v1.0.14 github.com/yyle88/eroticgo v0.0.2 github.com/yyle88/printgo v1.0.1 @@ -22,7 +23,6 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rogpeppe/go-internal v1.13.1 // indirect - github.com/yyle88/done v1.0.18 // indirect github.com/yyle88/formatgo v1.0.21 // indirect github.com/yyle88/must v0.0.10 // indirect github.com/yyle88/mutexmap v1.0.8 // indirect diff --git a/internal/utils/utils.go b/internal/utils/utils.go index f7db794..f10bf15 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -3,6 +3,7 @@ package utils import ( "fmt" + "github.com/yyle88/done" "github.com/yyle88/erero" "github.com/yyle88/eroticgo" ) @@ -27,21 +28,21 @@ func ShowWarning(message string) { // WarpMessage handles the output of the executed command and wraps errors. // WarpMessage 处理执行命令的输出,并在出现错误时封装错误信息。 -func WarpMessage(output []byte, err error, debugMode bool) ([]byte, error) { - if err != nil { +func WarpMessage(a *done.Vae[byte], debugMode bool) ([]byte, error) { + if a.E != nil { if debugMode { - if len(output) > 0 { - ShowWarning(string(output)) + if len(a.V) > 0 { + ShowWarning(string(a.V)) } else { - ShowWarning(err.Error()) + ShowWarning(a.E.Error()) } } - return output, erero.Wro(err) + return a.V, erero.Wro(a.E) } if debugMode { - if len(output) > 0 { - ShowMessage(string(output)) + if len(a.V) > 0 { + ShowMessage(string(a.V)) } } - return output, nil + return a.V, nil } diff --git a/osexec.go b/osexec.go index 4160f34..d2340cb 100644 --- a/osexec.go +++ b/osexec.go @@ -6,6 +6,7 @@ import ( "os/exec" "strings" + "github.com/yyle88/done" "github.com/yyle88/erero" "github.com/yyle88/osexec/internal/utils" "github.com/yyle88/zaplog" @@ -27,8 +28,7 @@ func Exec(name string, args ...string) ([]byte, error) { zaplog.ZAPS.P1.LOG.Debug("EXEC:", zap.String("CMD", debugMessage)) } command := exec.Command(name, args...) - output, err := command.CombinedOutput() - return utils.WarpMessage(output, err, debugModeOpen) + return utils.WarpMessage(done.VAE(command.CombinedOutput()), debugModeOpen) } // ExecInPath executes a command in a specified directory. @@ -50,8 +50,7 @@ func ExecInPath(path string, name string, args ...string) ([]byte, error) { } command := exec.Command(name, args...) command.Dir = path - output, err := command.CombinedOutput() - return utils.WarpMessage(output, err, debugModeOpen) + return utils.WarpMessage(done.VAE(command.CombinedOutput()), debugModeOpen) } // ExecInEnvs executes a command with custom environment variables. @@ -71,8 +70,7 @@ func ExecInEnvs(envs []string, name string, args ...string) ([]byte, error) { command := exec.Command(name, args...) command.Env = os.Environ() // Add custom environment variables command.Env = append(command.Env, envs...) - output, err := command.CombinedOutput() - return utils.WarpMessage(output, err, debugModeOpen) + return utils.WarpMessage(done.VAE(command.CombinedOutput()), debugModeOpen) } // ExecXshRun executes a command using a specific shell type and shell flag. @@ -96,8 +94,7 @@ func ExecXshRun(shellType, shellFlag string, name string, args ...string) ([]byt zaplog.ZAPS.P1.LOG.Debug("EXEC_XSH_RUN:", zap.String("CMD", debugMessage)) } command := exec.Command(shellType, "-c", name+" "+strings.Join(args, " ")) - output, err := command.CombinedOutput() - return utils.WarpMessage(output, err, debugModeOpen) + return utils.WarpMessage(done.VAE(command.CombinedOutput()), debugModeOpen) } // makeCommandMessage formats a command name and its arguments into a single command-line string. diff --git a/sure.gen.go b/sure.gen.go index 5a8065c..2c7e9e4 100644 --- a/sure.gen.go +++ b/sure.gen.go @@ -48,6 +48,11 @@ func (T *CommandConfig88Must) Exec(name string, args ...string) (res []byte) { sure.Must(err1) return res } +func (T *CommandConfig88Must) StreamExec(name string, args ...string) (res []byte) { + res, err1 := T.c.StreamExec(name, args...) + sure.Must(err1) + return res +} type CommandConfig88Soft struct{ c *CommandConfig } @@ -95,6 +100,11 @@ func (T *CommandConfig88Soft) Exec(name string, args ...string) (res []byte) { sure.Soft(err1) return res } +func (T *CommandConfig88Soft) StreamExec(name string, args ...string) (res []byte) { + res, err1 := T.c.StreamExec(name, args...) + sure.Soft(err1) + return res +} type CommandConfig88Omit struct{ c *CommandConfig } @@ -142,3 +152,8 @@ func (T *CommandConfig88Omit) Exec(name string, args ...string) (res []byte) { sure.Omit(err1) return res } +func (T *CommandConfig88Omit) StreamExec(name string, args ...string) (res []byte) { + res, err1 := T.c.StreamExec(name, args...) + sure.Omit(err1) + return res +}