[Agent] [Executor] 优化Functional Executor
This commit is contained in:
@@ -7,36 +7,11 @@ import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// ReadTimeCommandExecutor 执行命令,并且实时返回结果
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// AllCommandExecutor 正确或者错误的信息全部收集,共同返回
|
||||
func AllCommandExecutor(singleLineCommand []string) (bool, []string) {
|
||||
func AllCommandExecutor(singleLineCommand []string) (resultOk bool, resultLog []string) {
|
||||
// result
|
||||
var resultSlice []string
|
||||
resultOk := true
|
||||
resultOk = true
|
||||
|
||||
cmd := exec.Command(singleLineCommand[0], singleLineCommand[1:]...)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
@@ -69,6 +44,24 @@ func AllCommandExecutor(singleLineCommand []string) (bool, []string) {
|
||||
return resultOk, resultSlice
|
||||
}
|
||||
|
||||
// AllCompleteExecutor 多行命令的执行函数,返回执行正确与否和执行结果
|
||||
func AllCompleteExecutor(multiCommand [][]string) (resultOk bool, resultLog []string) {
|
||||
|
||||
var result []string
|
||||
resultOk = true
|
||||
|
||||
for _, singleLineCommand := range multiCommand {
|
||||
ok, resultLog := AllCommandExecutor(singleLineCommand)
|
||||
if !ok {
|
||||
// 执行出错
|
||||
resultOk = false
|
||||
}
|
||||
result = append(result, resultLog...)
|
||||
}
|
||||
|
||||
return resultOk, result
|
||||
}
|
||||
|
||||
// FormatAllCommandExecutor 收集全部执行结果、错误并且返回
|
||||
func FormatAllCommandExecutor(singleLineCommand []string) ([]string, error) {
|
||||
|
||||
@@ -107,6 +100,59 @@ func FormatAllCommandExecutor(singleLineCommand []string) ([]string, error) {
|
||||
return resultSlice, resultError
|
||||
}
|
||||
|
||||
// PureResultSingleExecute 执行单行命令,忽略输出,只对执行成功与否负责
|
||||
func PureResultSingleExecute(singleCommand []string) (resultOK bool) {
|
||||
|
||||
cmd := exec.Command(singleCommand[0], singleCommand[1:]...)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.ErrorF("指令 %s 执行 错误, 错误内容为 %s", singleCommand, err.Error())
|
||||
return false
|
||||
} else {
|
||||
log.DebugF("指令 %s 执行 成功", singleCommand)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// PureResultSingleExecuteBatch 批量 执行单行命令,忽略输出,只对执行成功与否负责
|
||||
func PureResultSingleExecuteBatch(singleCommandList [][]string) (resultOK bool) {
|
||||
|
||||
result := true
|
||||
|
||||
for _, singleCommand := range singleCommandList {
|
||||
if !PureResultSingleExecute(singleCommand) {
|
||||
result = false
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// ReadTimeCommandExecutor 执行命令,并且实时返回结果
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func realTimeOutput(r io.Reader) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
@@ -126,29 +172,3 @@ func collectOutput(r io.Reader, resultSlice []string) []string {
|
||||
|
||||
return resultSlice
|
||||
}
|
||||
|
||||
// PureResultSingleExecute 执行单行命令,忽略输出,只对执行成功与否负责
|
||||
func PureResultSingleExecute(singleCommand []string) bool {
|
||||
|
||||
cmd := exec.Command(singleCommand[0], singleCommand[1:]...)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.ErrorF("指令 %s 执行 错误, 错误内容为 %s", singleCommand, err.Error())
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user