117 lines
3.0 KiB
Go
117 lines
3.0 KiB
Go
package executor
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"os/exec"
|
|
)
|
|
|
|
func ReadTimeCommandExecutor(singleLineCommand []string) {
|
|
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
|
stdout, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
log.ErrorF("command %v stdout error => %v", singleLineCommand, err)
|
|
}
|
|
stderr, err := cmd.StderrPipe()
|
|
if err != nil {
|
|
log.ErrorF("command %v stderr error => %v", singleLineCommand, err)
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
|
|
}
|
|
|
|
go realTimeOutput(stdout)
|
|
go realTimeOutput(stderr)
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
log.ErrorF("command %v result error => %v", singleLineCommand, err)
|
|
}
|
|
|
|
}
|
|
|
|
// AllOutputCommandExecutor 收集全部执行结果、错误并且返回
|
|
func AllOutputCommandExecutor(singleLineCommand []string) ([]string, error) {
|
|
|
|
// result
|
|
var resultSlice []string
|
|
var resultError error
|
|
|
|
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
|
stdout, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
log.ErrorF("command %v stdout error => %v", singleLineCommand, err)
|
|
resultError = err
|
|
}
|
|
stderr, err := cmd.StderrPipe()
|
|
if err != nil {
|
|
log.ErrorF("command %v stderr error => %v", singleLineCommand, err)
|
|
resultError = err
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
log.ErrorF("command %v runtime error => %v", singleLineCommand, err)
|
|
}
|
|
|
|
resultSlice = append(resultSlice, fmt.Sprintf(" ========= 命令为 ====> %v", singleLineCommand), "↓↓↓ 命令 输出 如下 ↓↓↓")
|
|
resultSlice = collectOutput(stdout, resultSlice)
|
|
resultSlice = append(resultSlice, "↓↓↓ 命令 错误 如下 ↓↓↓")
|
|
resultSlice = collectOutput(stderr, resultSlice)
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
log.ErrorF("command %v result error => %v", singleLineCommand, err)
|
|
resultError = err
|
|
}
|
|
|
|
log.DebugF("real time exec result are %v", resultSlice)
|
|
|
|
return resultSlice, resultError
|
|
}
|
|
|
|
func realTimeOutput(r io.Reader) {
|
|
scanner := bufio.NewScanner(r)
|
|
for scanner.Scan() {
|
|
fmt.Println(scanner.Text())
|
|
}
|
|
}
|
|
|
|
func collectOutput(r io.Reader, resultSlice []string) []string {
|
|
scanner := bufio.NewScanner(r)
|
|
for scanner.Scan() {
|
|
resultLine := scanner.Text()
|
|
|
|
resultSlice = append(resultSlice, resultLine)
|
|
// debug usage
|
|
//fmt.Println(resultLine)
|
|
}
|
|
|
|
return resultSlice
|
|
}
|
|
|
|
// PureResultSingleExecute 执行单行命令,忽略输出,只对执行成功与否负责
|
|
func PureResultSingleExecute(singleCommand []string) bool {
|
|
|
|
cmd := exec.Command(singleCommand[0], singleCommand[1:]...)
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
log.DebugF("指令 %s 执行 错误", singleCommand)
|
|
return false
|
|
} else {
|
|
log.DebugF("指令 %s 执行 成功", singleCommand)
|
|
return true
|
|
}
|
|
}
|
|
|
|
// PureResultSingleExecuteBatch 批量 执行单行命令,忽略输出,只对执行成功与否负责
|
|
func PureResultSingleExecuteBatch(singleCommandList [][]string) bool {
|
|
|
|
for _, singleCommand := range singleCommandList {
|
|
if !PureResultSingleExecute(singleCommand) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|