Skip to content

Commit 96290e7

Browse files
committed
✨ tail
1 parent 553fec5 commit 96290e7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tail/tail.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tail
2+
3+
import (
4+
"bufio"
5+
"io"
6+
"log"
7+
"os/exec"
8+
)
9+
10+
// 简单封装tail命令
11+
func Tail(filePath string, f func(text string)) {
12+
c := exec.Command("tail", "-f", "-n", "+0", filePath)
13+
stdout, err := c.StdoutPipe()
14+
if err != nil {
15+
log.Fatalln(err)
16+
}
17+
if err = c.Start(); err != nil {
18+
log.Fatalln(err)
19+
}
20+
go func(stdout io.ReadCloser) {
21+
buf := bufio.NewScanner(stdout)
22+
buf.Split(bufio.ScanLines)
23+
for buf.Scan() {
24+
f(buf.Text())
25+
}
26+
}(stdout)
27+
}

0 commit comments

Comments
 (0)