exec.go 871 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package util
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. "syscall"
  9. )
  10. var env []string
  11. func init() {
  12. cp, err := filepath.Abs(".")
  13. if err != nil {
  14. panic(err)
  15. }
  16. for _, v := range os.Environ() {
  17. if strings.HasPrefix(v, "GOPATH=") {
  18. env = append(env, v+":"+cp)
  19. } else {
  20. env = append(env, v)
  21. }
  22. }
  23. }
  24. // Exec execute go commang with GOPATH=$GOPATH:`pwd`
  25. func Exec(params ...string) {
  26. var err error
  27. fmt.Printf("run: %s", strings.Join(params, " "))
  28. cmd := exec.Command(params[0], params[1:]...)
  29. cmd.Env = env
  30. cmd.Stdin = os.Stdin
  31. cmd.Stdout = os.Stdout
  32. cmd.Stderr = os.Stderr
  33. if err = cmd.Start(); err != nil {
  34. panic(err)
  35. }
  36. if err = cmd.Wait(); err != nil {
  37. if exiterr, ok := err.(*exec.ExitError); ok {
  38. if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
  39. os.Exit(status.ExitStatus())
  40. }
  41. }
  42. }
  43. fmt.Println()
  44. }