【问题标题】:golang exec.Command in different foldergolang exec.Command 在不同的文件夹中
【发布时间】:2017-08-14 12:00:45
【问题描述】:

我对分配了 .Dir 的 exec.Command 有疑问。当我从应用程序运行命令时,出现错误。但是如果我从 shell 运行它,同样的命令就可以正常工作。

command := exec.Command("git", "rev-parse", "--verify", "tags/v1.0.0^{commit}")
command.Dir = "sub/subdir"
out, err := command.CombinedOutput()
fmt.Printf("Executed command [%s] %s\nErrorCode = %s\nOutput = %s\n", command.Dir, command.Args, err, out)

输出:

Executed command [sub/subdir] [git rev-parse --verify tags/v1.0.0^{commit}]
ErrorCode = exit status 128 
Output = fatal: Needed a single revision

外壳命令:

$ (cd sub/subdir; git rev-parse --verify tags/v1.0.0^{commit})
c1f3b8707ac001dab875781def3c729e3ed6de2c

我做错了什么?

【问题讨论】:

    标签: go


    【解决方案1】:

    使用绝对路径。查看filepath.Abs

    【讨论】:

    • 或者更好的是,尝试使用filepath.Abs 构建绝对路径。打印该值以确保您使用的是您期望的路径。
    • 它没有帮助 - 结果相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多