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

huangapple go评论93阅读模式
英文:

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

问题

我正在使用glide来管理我的项目依赖。我创建了一个脚本来运行go test $(glide novendor)(测试所有目录,排除_vendor/_目录)。虽然它可以工作,但运行命令的输出只显示第一行:

ok my/project/scripts 0.005s

以下是运行该脚本的部分代码:

// 获取要测试的路径(排除"vendor/"目录)
cmd := exec.Command("glide", "novendor")
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
    log.Fatal("无法运行 `glide novendor`: ", err)
}
glidenovendor := []string{"test;"}
// 表示命令中的 "test ./src/... ./scripts/..." 部分
glidenovendor = append(glidenovendor, strings.Split(out.String(), " ")...)

// 运行 `go test ./src/... ./scripts/...`
cmd = exec.Command("go", glidenovendor...)
cmd.Stdout = os.Stdout
err = cmd.Run()
if err != nil {
    log.Fatal("无法使用参数运行 `go test` 命令: ", cmd, err)
}

直接在命令行中运行该命令可以得到预期的所有输出行。

如何使我的脚本打印完整的输出?

英文:

I'm using glide to manage dependencies on my project. I created a script that runs go test $(glide novendor) (which tests all directories, excluding the vendor/ one) for me. While it works, the output for the run command doesn't go beyond the 1st line:

ok my/project/scripts 0.005s

Here is the portion of the script that runs it:

<!-- language: lang-go -->

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

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

Running the command directly on my shell gives me all lines of out put as expected.

How do I make my script print the entire output?

答案1

得分: 0

原文翻译如下:

原来,在这行代码中:

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

由于某种原因,会在glidenovendor切片的最后一个字符串元素中添加一个换行符"\n"。我仍然不知道为什么会这样。但是使用下面的代码片段将其移除后,脚本按预期运行:

// 从`glidenovendor`中的字符串中移除尾随的换行符
for i, v := range glidenovendor {
    glidenovendor[i] = strings.Trim(v, "\n")
}
英文:

It turns out that line

glidenovendor = append(glidenovendor, strings.Split(out.String(), &quot; &quot;)...)

for some reason added a new line character &quot;\n&quot; to the last string element of the glidenovendor slice. I still have no idea why. But removing it with the snippet below got the script running as expected:

<!-- language: lang-go -->

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

huangapple
  • 本文由 发表于 2015年11月9日 16:18:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/33604928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定