2017-03-22 33 views
1

我用exec.Command與.Dir分配有問題。當我從應用程序運行命令時,出現錯誤。但如果我從shell運行它,相同的命令工作正常。golang exec.Command在不同的文件夾中

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 

Shell命令:

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

我做了什麼錯?

回答

0

使用絕對路徑。退房filepath.Abs

+0

或者更好的是,嘗試使用['filepath.Abs​​'](https://golang.org/pkg/path/filepath/#Abs)來構建絕對路徑。打印該值以確保您使用的是期望的路徑。 – cd1

+0

它沒有幫助 - 相同的結果。 – Michael

相關問題