41 lines
751 B
Go
41 lines
751 B
Go
package lib
|
|
|
|
import (
|
|
"bytes"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
func Exec(command string, envs []string) (out string, err error) {
|
|
out, err = execInternal(command, envs, true)
|
|
|
|
return
|
|
}
|
|
|
|
func ExecNotFatal(command string, envs []string) (out string, err error) {
|
|
out, err = execInternal(command, envs, false)
|
|
|
|
return
|
|
}
|
|
|
|
func execInternal(command string, envs []string, failOnError bool) (out string, err error) {
|
|
exec := exec.Command("bash", "-c", command)
|
|
|
|
exec.Env = os.Environ()
|
|
exec.Env = append(exec.Env, envs...)
|
|
|
|
var buffer bytes.Buffer
|
|
exec.Stdout = &buffer
|
|
exec.Stderr = &buffer
|
|
|
|
err = exec.Run()
|
|
|
|
if failOnError && err != nil {
|
|
log.Fatal("can't execute, " + err.Error() + ": " + buffer.String())
|
|
}
|
|
|
|
out = buffer.String()
|
|
|
|
return
|
|
}
|