Skip to content

Commit

Permalink
使用done包把两个返回值合起来
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Dec 16, 2024
1 parent cb31b8d commit c768a81
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
8 changes: 4 additions & 4 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 10 additions & 9 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"fmt"

"github.com/yyle88/done"
"github.com/yyle88/erero"
"github.com/yyle88/eroticgo"
)
Expand All @@ -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
}
13 changes: 5 additions & 8 deletions osexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions sure.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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
}

0 comments on commit c768a81

Please sign in to comment.