I have a pretty basic golang question.
Given an arbitrary string like "echo foo"
, or "CGO_ENABLED=0 go build -o ./bin/echo -a main.go"
, what is the idiomatic way to parse/run that command using os/exec
?
The way that I got this working seems pretty hacky, and I'm looking for a more idiomatic approach.
userInput = "CGO_ENABLED=0 go build -o ./bin/echo -a main.go"
command := exec.Command("/bin/bash", "-c", userInput)
out, err := command.Output()
if err != nil {
fmt.Println(err)
}
Depending on how arbitrary and flexible your userInput
string is, I might do something like this.
package main
import (
"log"
"os"
"os/exec"
)
func main() {
if err := os.Setenv("FOO", "bar"); err != nil {
log.Println(err)
}
// As Dave C pointed out, exec.Command does this for you.
// goAbsPath, err := exec.LookPath("go")
// if err != nil {
// log.Println(err)
// }
// Pass whatever other arguments necessary.
cmd := exec.Command("go", "version")
if err = cmd.Run(); err != nil {
log.Println(err)
}
}
If nothing else, maybe os.Setenv
and exec.LookPath
might be useful to know about.
I may write like this:
cmd := exec.Command(cmdName, arg1, arg2, ..)
cmd.Env = append(cmd.Env, "foo=bar")
err = cmd.Start()
if err != nil {
// log or something else
}
err = cmd.Wait()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
// handle err according to different status.ExitStatus()
}
// ...
}
// ...
}
// ...
Godoc explains quite well if you need more details.