优化返回结果

This commit is contained in:
白茶清欢 2022-05-20 22:31:29 +08:00
parent a015d2984d
commit d4e7b563bc
3 changed files with 7 additions and 1 deletions

2
.gitignore vendored
View File

@ -18,5 +18,5 @@
# vendor/ # vendor/
.idea .idea
.vscode .vscode
mail_test.go *_test.go

View File

@ -13,6 +13,8 @@ package define
// //
// Date : 22:00 2022/5/20 // Date : 22:00 2022/5/20
type Result struct { type Result struct {
WorkDir string // 执行的目录
CmdPath string // 命令的路径
Err error // 异常信息 Err error // 异常信息
Output []byte // 输出的内容 Output []byte // 输出的内容
StartTime int64 // 开始时间 StartTime int64 // 开始时间

View File

@ -22,6 +22,7 @@ import (
func Execute(workDir string, command string, param []string) define.Result { func Execute(workDir string, command string, param []string) define.Result {
//执行命令 //执行命令
result := define.Result{ result := define.Result{
WorkDir: workDir,
Err: nil, Err: nil,
Output: nil, Output: nil,
StartTime: time.Now().UnixNano(), StartTime: time.Now().UnixNano(),
@ -30,6 +31,9 @@ func Execute(workDir string, command string, param []string) define.Result {
defer func() { defer func() {
result.FinishTime = time.Now().UnixNano() result.FinishTime = time.Now().UnixNano()
}() }()
if result.CmdPath, result.Err = Check(command); nil != result.Err {
return result
}
cmdInstance := exec.Command(command, param...) cmdInstance := exec.Command(command, param...)
cmdInstance.Dir = workDir cmdInstance.Dir = workDir
result.Output, result.Err = cmdInstance.CombinedOutput() result.Output, result.Err = cmdInstance.CombinedOutput()