我试过使用stdoutpipe 和 stderrpipe,如下所示。shell := exec.Command("npm", args...)shell.Dir = cwdoutpipe, _ := shell.StdoutPipe()errpipe, _ := shell.StderrPipe()shell.Start()go func(pipe io.ReadCloser) { reader := bufio.NewReader(pipe) line, err := reader.ReadString('\n') for err == nil { fmt.Println(string(line)) line, err = reader.ReadString('\n') } fmt.Println("exited")}(outpipe)go func(pipe io.ReadCloser) { reader := bufio.NewReader(pipe) line, err := reader.ReadString('\n') for err == nil { fmt.Println(string(line)) line, err = reader.ReadString('\n') } fmt.Println("exited")}(errpipe)err := shell.Wait()if err != nil { fmt.Println(err)}但是我只得到输出:added 87 packages, and audited 88 packages in 3s9 packages are looking for fundingrun `npm fund` for detailsfound 0 vulnerabilities如何获取从控制台运行 npm install 时获得的安装进度条?我也尝试过使用下面的解决方案并在运行 shell.run() 后打印缓冲区,但它也提供与上面相同的输出。shell := exec.Command(command, args...)shell.Dir = cwdvar stderr, stdout bytes.Buffershell.Stderr = &stderrshell.Stdout = &stdoutreturn shell, &stdout, &stderr
1 回答
慕尼黑5688855
TA贡献1848条经验 获得超2个赞
cmd := exec.Command("npm", args...)
cmd.Dir = cwd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
重定向到os 的 stdout 和 stderr就是诀窍。
- 1 回答
- 0 关注
- 145 浏览
添加回答
举报
0/150
提交
取消