[Agent] [Executor] 优化Functional Executor
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// ReadTimeCommandExecutor 执行命令,并且实时返回结果
|
||||
func ReadTimeCommandExecutor(singleLineCommand []string) {
|
||||
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
@@ -31,8 +32,45 @@ func ReadTimeCommandExecutor(singleLineCommand []string) {
|
||||
|
||||
}
|
||||
|
||||
// AllOutputCommandExecutor 收集全部执行结果、错误并且返回
|
||||
func AllOutputCommandExecutor(singleLineCommand []string) ([]string, error) {
|
||||
// AllCommandExecutor 正确或者错误的信息全部收集,共同返回
|
||||
func AllCommandExecutor(singleLineCommand []string) (bool, []string) {
|
||||
// result
|
||||
var resultSlice []string
|
||||
resultOk := true
|
||||
|
||||
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.DebugF("command %v stdout error => %v", singleLineCommand, err)
|
||||
resultOk = false
|
||||
}
|
||||
stderr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
log.DebugF("command %v stderr error => %v", singleLineCommand, err)
|
||||
resultOk = false
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
log.DebugF("command %v runtime error => %v", singleLineCommand, err)
|
||||
resultOk = false
|
||||
}
|
||||
|
||||
// 收集错误或者
|
||||
resultSlice = collectOutput(stdout, resultSlice)
|
||||
resultSlice = collectOutput(stderr, resultSlice)
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
log.DebugF("command %v result error => %v", singleLineCommand, err)
|
||||
resultOk = false
|
||||
}
|
||||
|
||||
log.DebugF("all out command executor result are => %v", resultSlice)
|
||||
|
||||
return resultOk, resultSlice
|
||||
}
|
||||
|
||||
// FormatAllCommandExecutor 收集全部执行结果、错误并且返回
|
||||
func FormatAllCommandExecutor(singleLineCommand []string) ([]string, error) {
|
||||
|
||||
// result
|
||||
var resultSlice []string
|
||||
@@ -54,7 +92,7 @@ func AllOutputCommandExecutor(singleLineCommand []string) ([]string, error) {
|
||||
log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
|
||||
}
|
||||
|
||||
resultSlice = append(resultSlice, fmt.Sprintf(" ========= 命令为 ====> %v", singleLineCommand), "↓↓↓ 命令 输出 如下 ↓↓↓")
|
||||
resultSlice = append(resultSlice, fmt.Sprintf(" 开始执行命令 ====> %s", singleLineCommand), "↓↓↓ 命令 输出 如下 ↓↓↓")
|
||||
resultSlice = collectOutput(stdout, resultSlice)
|
||||
resultSlice = append(resultSlice, "↓↓↓ 命令 错误 如下 ↓↓↓")
|
||||
resultSlice = collectOutput(stderr, resultSlice)
|
||||
@@ -95,7 +133,7 @@ func PureResultSingleExecute(singleCommand []string) bool {
|
||||
cmd := exec.Command(singleCommand[0], singleCommand[1:]...)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.DebugF("指令 %s 执行 错误", singleCommand)
|
||||
log.ErrorF("指令 %s 执行 错误, 错误内容为 %s", singleCommand, err.Error())
|
||||
return false
|
||||
} else {
|
||||
log.DebugF("指令 %s 执行 成功", singleCommand)
|
||||
|
||||
Reference in New Issue
Block a user