当 运行 使用 exec.Cmd.Run() 命令时,如何获取所有输出行?

How do I get all lines of output when running command with exec.Cmd.Run()?

我正在使用 glide 来管理我的项目的依赖项。我为我创建了一个 运行s go test $(glide novendor) 脚本(测试所有目录,不包括 vendor/ 目录)。虽然它有效,但 运行 命令的输出不会超出第一行:

ok my/project/scripts 0.005s

这是脚本的 运行 部分:

// Get the paths to test (excluding the "vendor/" directory)
cmd := exec.Command("glide", "novendor")
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
    log.Fatal("Could not run `glide novendor`: ", err)
}
glidenovendor := []string{"test"}
// Represents the "test ./src/... ./scripts/..." portion of the command
glidenovendor = append(glidenovendor, strings.Split(out.String(), " ")...)

// Run `go test ./src/... ./scripts/...`
cmd = exec.Command("go", glidenovendor...)
cmd.Stdout = os.Stdout
err = cmd.Run()
if err != nil {
    log.Fatal("Could not run `go test` command with args: ", cmd, err)
}

运行 直接在我的 shell 上的命令给了我预期的所有输出行。

如何让我的脚本打印整个输出?

原来那行

glidenovendor = append(glidenovendor, strings.Split(out.String(), " ")...)

出于某种原因,向 glidenovendor 切片的最后一个字符串元素添加了换行符 "\n"。我仍然不知道为什么。但是用下面的 片段删除它 得到了预期的脚本 运行:

// Remove trailing LF from strings in `glidenovendor`
for i, v := range glidenovendor {
    glidenovendor[i] = strings.Trim(v, "\n")
}