dshgdhdfcas30210
dshgdhdfcas30210
2015-03-11 22:57

exec.command设置输出流未获取所有数据

已采纳

This works

res = exec.Command(gitCmd, cmdArgs...)
res.Stdout, res.Stderr = os.Stdout,os.Stderr

When executing a git command like git clone .. you get full syntax response like

remote: Counting objects: 15, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 15 (delta 4), reused 0 (delta 0)
Receiving objects: 100% (15/15), done.
Resolving deltas: 100% (4/4), done.

BUT if you wrap it in a multiwriter like this

res = exec.Command(gitCmd, cmdArgs...)
res.Stdout, res.Stderr = io.MultiWriter(os.Stdout),io.MultiWriter(os.Stderr)

You lose all the resolving / receiving outputs, that you normally see. Why ?

The following works but to me seems unnecessary, IMHO the io.MultiWriter should do the same ...

res = exec.Command(gitCmd, cmdArgs...)
res.Stdout, res.Stderr = os.Stdout,os.Stderr
stdout, stderr = new(bytes.Buffer), new(bytes.Buffer)
go io.Copy(os.Stdout,stdout)
go io.Copy(os.Stderr,stderr)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doujiu4643 doujiu4643 6年前

    The difference is the TTY. Using a MultiWriter (or anything else) would break the TTY. If you want to do so, you need to use a PTY master/slave pair. You can take a look at https://github.com/kr/pty for more info.

    点赞 评论 复制链接分享

相关推荐