[Agent] [Executor] 完成PipelineCommand part

This commit is contained in:
zeaslity
2023-10-18 10:39:38 +08:00
parent be01eb28a9
commit f31b303ff8
4 changed files with 151 additions and 25 deletions

View File

@@ -102,37 +102,53 @@ func FormatAllCommandExecutor(singleLineCommand []string) ([]string, error) {
return resultSlice, resultError
}
func PipelineCommandExecutor() {
cmd1 := exec.Command("ps", "aux")
cmd2 := exec.Command("grep", "apipe")
func PipelineCommandExecutor(pipelineCommand [][]string) (resultOk bool, resultLog []string) {
if len(pipelineCommand) == 0 {
return true, nil
} else if len(pipelineCommand) == 1 {
log.Debug("输入的PipelineCommand长度有误")
return AllCommandExecutor(pipelineCommand[0])
}
var c []string
cmd1 := exec.Command(pipelineCommand[0][0], pipelineCommand[0][1:]...)
var outputBuf1 bytes.Buffer
cmd1.Stdout = &outputBuf1
if err := cmd1.Start(); err != nil {
fmt.Printf("Error: The first command can not be startup %s\n", err)
return
sprintf := fmt.Sprintf("Error: The first command can not be startup %s", err)
return false, append(c, sprintf)
}
if err := cmd1.Wait(); err != nil {
fmt.Printf("Error: Couldn't wait for the first command: %s\n", err)
return
}
cmd2.Stdin = &outputBuf1
var outputBuf2 bytes.Buffer
cmd2.Stdout = &outputBuf2
if err := cmd2.Start(); err != nil {
fmt.Printf("Error: The second command can not be startup: %s\n", err)
return
}
if err := cmd2.Wait(); err != nil {
fmt.Printf("Error: Couldn't wait for the second command: %s\n", err)
return
sprintf := fmt.Sprintf("Error: Couldn't wait for the first command: %s", err)
return false, append(c, sprintf)
}
s := outputBuf2.String()
for i := 1; i < len(pipelineCommand); i++ {
cmd2 := exec.Command(pipelineCommand[i][0], pipelineCommand[i][1:]...)
sprintf := fmt.Sprintf("current command is %s", pipelineCommand[i])
c = append(c, sprintf)
cmd2.Stdin = &outputBuf1
var outputBuf2 bytes.Buffer
cmd2.Stdout = &outputBuf2
if err := cmd2.Start(); err != nil {
sprintf := fmt.Sprintf("Error: The second command can not be startup: %s", err)
return false, append(c, sprintf)
}
if err := cmd2.Wait(); err != nil {
sprintf := fmt.Sprintf("Error: Couldn't wait for the second command: %s", err)
return false, append(c, sprintf)
}
// change
outputBuf1 = outputBuf2
}
s := outputBuf1.String()
split := strings.Split(s, "\n")
BasicPrettyPrint(true, split)
return true, split
}
// PureResultSingleExecute 执行单行命令,忽略输出,只对执行成功与否负责