-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
52 lines (42 loc) · 913 Bytes
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package minidoc
import (
"bufio"
"fmt"
"os/exec"
)
// Execute execute
func Execute(args []string) (string, error) {
output, err := exec.Command(args[0], args[1:]...).Output()
if err != nil {
fmt.Println(err)
return "", err
}
return string(output), err
}
// Exec exec.Command
func Exec(args []string) error {
cmd := exec.Command(args[0], args[1:]...)
stdOut, _ := cmd.StdoutPipe()
stdErr, _ := cmd.StderrPipe()
//stdIn, _ := cmd.StdinPipe()
err := cmd.Start()
if err != nil {
fmt.Println("terminated early: " + err.Error())
return err
}
go func() {
scanner := bufio.NewScanner(stdOut)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}()
go func() {
scanner := bufio.NewScanner(stdErr)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}()
// if exec.Command calls a service that blocks then this code will never be reached
err = cmd.Wait()
return err
}