1 Star 0 Fork 0

litian / machine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
stdout_capture.go 690 Bytes
一键复制 编辑 原始数据 按行查看 历史
David Gageot 提交于 2015-11-25 13:41 . Add more command tests
package commandstest
import (
"bytes"
"io"
"os"
)
var (
stdout *os.File
)
func init() {
stdout = os.Stdout
}
type StdoutGetter interface {
Output() string
Stop()
}
type stdoutCapturer struct {
stdout *os.File
output chan string
}
func NewStdoutGetter() StdoutGetter {
r, w, _ := os.Pipe()
os.Stdout = w
output := make(chan string)
go func() {
var testOutput bytes.Buffer
io.Copy(&testOutput, r)
output <- testOutput.String()
}()
return &stdoutCapturer{
stdout: w,
output: output,
}
}
func (c *stdoutCapturer) Output() string {
c.stdout.Close()
text := <-c.output
close(c.output)
return text
}
func (c *stdoutCapturer) Stop() {
os.Stdout = stdout
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/litian33/machine.git
git@gitee.com:litian33/machine.git
litian33
machine
machine
v0.5.4

搜索帮助

344bd9b3 5694891 D2dac590 5694891