-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.go
More file actions
57 lines (46 loc) · 870 Bytes
/
execute.go
File metadata and controls
57 lines (46 loc) · 870 Bytes
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
53
54
55
56
57
package main
import (
"bufio"
"context"
"os/exec"
"time"
log "github.com/sirupsen/logrus"
)
func executeShell(shell string) {
if shell == "" {
return
}
go execute(shell)
}
func execute(shell string) {
ctx, calcel := context.WithTimeout(context.Background(), 10*time.Minute)
defer calcel()
cmd := exec.CommandContext(ctx, "sh", "-c", shell)
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
oReader := bufio.NewReader(stdout)
eReader := bufio.NewReader(stderr)
scan(oReader, log.Info)
scan(eReader, log.Info)
err := cmd.Start()
if err != nil {
log.Error(err)
}
err = cmd.Wait()
if err != nil {
log.Error(err)
}
}
func scan(reader *bufio.Reader, f func(args ...interface{})) {
go func() {
for {
line, err := reader.ReadString('\n')
if len(line) > 0 {
f(line)
}
if err != nil {
break
}
}
}()
}