Skip to content

Commit

Permalink
增加执行命令并实时输出日志函数
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl committed Mar 23, 2023
1 parent 56a7d5f commit 674df95
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions analysis-tool-sdk-golang/util/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package util

import (
"bufio"
"os/exec"
)

// ExecAndLog 执行命令并实时输出日志
func ExecAndLog(name string, args []string) error {
cmd := exec.Command(name, args...)
Info("will execute: %s", cmd.String())

outScanner, errScanner, err := createScanner(cmd)
if err != nil {
return err
}

if err := cmd.Start(); err != nil {
return err
}

go func() {
for errScanner.Scan() {
Info(errScanner.Text() + "\n")
}
}()

go func() {
for outScanner.Scan() {
Info(outScanner.Text() + "\n")
}
}()

if err := cmd.Wait(); err != nil {
return err
}
return nil
}

func createScanner(cmd *exec.Cmd) (*bufio.Scanner, *bufio.Scanner, error) {
outPipe, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
errPipe, err := cmd.StderrPipe()
if err != nil {
return nil, nil, err
}

outScanner := bufio.NewScanner(outPipe)
outScanner.Split(bufio.ScanLines)
errScanner := bufio.NewScanner(errPipe)
errScanner.Split(bufio.ScanLines)

return outScanner, errScanner, nil
}

0 comments on commit 674df95

Please sign in to comment.