[agent-go] update pipline command executor
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ExecutionMessage struct {
|
||||
@@ -54,42 +55,20 @@ func Execute(em *ExecutionMessage) ([]string, error) {
|
||||
|
||||
func PipeLineCommandExecutor(pipeLineCommand [][]string) ([]string, error) {
|
||||
|
||||
var cmds []*exec.Cmd
|
||||
|
||||
// 创建每个命令对象,并将前一个命令的标准输出连接到当前命令的标准输入
|
||||
for i, partOfCommand := range pipeLineCommand {
|
||||
cmd := exec.Command(partOfCommand[0], partOfCommand[1:]...)
|
||||
if i > 0 {
|
||||
prevCmd := cmds[i-1]
|
||||
out, err := prevCmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.ErrorF("Pipeline command error happened! Command is => %v, Error is %v", partOfCommand, err)
|
||||
return nil, err
|
||||
}
|
||||
cmd.Stdin = out
|
||||
var output []byte
|
||||
var err error
|
||||
for i, command := range pipeLineCommand {
|
||||
cmd := exec.Command(command[0], command[1:]...)
|
||||
cmd.Stdin = bytes.NewReader(output)
|
||||
output, err = cmd.Output()
|
||||
if err != nil {
|
||||
return strings.Split(string(output), "\n"), err
|
||||
}
|
||||
if i == len(pipeLineCommand)-1 {
|
||||
return strings.Split(string(output), "\n"), nil
|
||||
}
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
|
||||
// 执行最后一个命令,并获取其输出
|
||||
lastCmd := cmds[len(cmds)-1]
|
||||
|
||||
var out bytes.Buffer
|
||||
lastCmd.Stdout = &out
|
||||
lastCmd.Stderr = &out
|
||||
err := lastCmd.Run()
|
||||
|
||||
scanner := bufio.NewScanner(&out)
|
||||
var result []string
|
||||
for scanner.Scan() {
|
||||
result = append(result, scanner.Text())
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
func MultiLineCommandExecutor(multiLineCommandExecutor [][]string) ([]string, error) {
|
||||
|
||||
Reference in New Issue
Block a user