[Excution] - base function accomplish - 4

This commit is contained in:
IceDerce
2023-06-26 13:50:14 +08:00
parent 80198930c4
commit 97187363cc
6 changed files with 88 additions and 30 deletions

View File

@@ -2,12 +2,11 @@ package executor
import (
"bufio"
"fmt"
"io"
"os/exec"
)
func ReadTimeCommandExecutor(singleLineCommand []string) {
func ReadTimeCommandExecutor(singleLineCommand []string) []string {
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
stdout, err := cmd.StdoutPipe()
@@ -23,18 +22,28 @@ func ReadTimeCommandExecutor(singleLineCommand []string) {
log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
}
go copyOutput(stdout)
go copyOutput(stderr)
var resultSlice []string
resultSlice = append(resultSlice, copyOutput(stdout, resultSlice)...)
resultSlice = append(resultSlice, copyOutput(stderr, resultSlice)...)
if err := cmd.Wait(); err != nil {
log.ErrorF("command %v result error => %v", singleLineCommand, err)
}
//log.DebugF("real time exec result are %v", resultSlice)
return resultSlice
}
func copyOutput(r io.Reader) {
func copyOutput(r io.Reader, resultSlice []string) []string {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
fmt.Println(scanner.Text())
resultLine := scanner.Text()
resultSlice = append(resultSlice, resultLine)
// debug usage
//fmt.Println(resultLine)
}
return resultSlice
}