使用golang的exec.Command运行go install。

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

running go install with golang exec.Command

问题

我正在尝试为一个 webhook 编写一些代码,该代码将调用 go install。我遇到的问题是,在调用任何 go 命令时,exec.Command 中的 GOPATH 没有设置。

当 webhook 端点被触发时,会出现以下错误:

exit status 1: can't load package: package github.com/me/myrepo/mything: cannot find package "github.com/me/myrepo/mything" in any of:
	/usr/lib/go-1.6/src/github.com/me/myrepo/mything (from $GOROOT)
	($GOPATH not set)

在这种情况下,我该如何确保设置 GOPATH?

如果我从命令行运行 "go install github.com/me/myrepo/mything",它可以正常工作。

英文:

I'm trying to write some code for a webhook, that will call go install. The problem i'm having is that the GOPATH isn't set when i call any go commands with exec.Command

func exec_cmd(w http.ResponseWriter, cmd string, args ...string) {
	command := exec.Command(cmd, args...)
	var out bytes.Buffer
	var stderr bytes.Buffer
	command.Stdout = &out
	command.Stderr = &stderr
	err := command.Run()
	if err != nil {
 		errstring := fmt.Sprintf(fmt.Sprint(err) + ": " + stderr.String())
 		io.WriteString(w, errstring)
	}
	io.WriteString(w, out.String())
	fmt.Println(out.String())
}

func webhook(w http.ResponseWriter, r *http.Request) {
	exec_cmd(w, "go", "install", "github.com/me/myrepo/mything")
}

func test(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "test")
}

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/webhook", webhook)
	mux.HandleFunc("/", test)
	http.ListenAndServe(":8000", mux)
}

when the webhook endpoint is hit, it gives:

exit status 1: can't load package: package github.com/me/myrepo/mything: cannot find package "github.com/me/myrepo/mything" in any of:
	/usr/lib/go-1.6/src/github.com/me/myrepo/mything (from $GOROOT)
	($GOPATH not set)

How would i go about making sure the GOPATH is set in this context?

If i run "go install github.com/me/myrepo/mything" from the command line, it works fine.

答案1

得分: 2

你是在你的编辑器环境中运行这个代码吗,还是在一个容器中?如果在没有设置GOPATH环境变量的上下文中运行,它是不会工作的。

如果使用"go run main.go"运行,它能工作吗?在那个上下文中,我没有修改你的代码就能运行。只要父上下文可以访问GOPATH,它就应该能工作。你也可以手动设置它,例如:

command.Env = append(os.Environ(), "GOPATH=/tmp/go")

或者你可以在这个进程将要运行的上下文中设置GOPATH(用于安装)和PATH(用于go、git命令),例如在一个systemd单元文件中。

英文:

Are you running this in your editor context, or in a container perhaps? It won't work in a context without the GOPATH env variable set.

If running with go run main.go, does it work? It works for me in that context without modifying your code. As long as the parent context has access to GOPATH it should. You could alternatively set it manually with something like this:

command.Env = append(os.Environ(), "GOPATH=/tmp/go")

Or you could set GOPATH (for install) and PATH (for go,git cmds) in the context this process will run in (probably preferable), for example in a systemd unit file.

huangapple
  • 本文由 发表于 2017年3月21日 19:16:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/42925551.html
匿名

发表评论

匿名网友

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

确定