Пример #1
0
func buildAndRun() (status int, err error) {
	// will support Ansi colors for windows
	stdout := ansicolor.NewAnsiColorWriter(os.Stdout)
	buffer := bytes.NewBuffer([]byte(""))
	stderr := ansicolor.NewAnsiColorWriter(buffer)

	builtFile := fmt.Sprintf("%s/%dgodog.go", os.TempDir(), time.Now().UnixNano())

	buf, err := godog.Build()
	if err != nil {
		return
	}

	w, err := os.Create(builtFile)
	if err != nil {
		return
	}
	defer os.Remove(builtFile)

	if _, err = w.Write(buf); err != nil {
		w.Close()
		return
	}
	w.Close()

	cmd := exec.Command("go", append([]string{"run", builtFile}, os.Args[1:]...)...)
	cmd.Stdout = stdout
	cmd.Stderr = stderr

	defer func() {
		s := strings.TrimSpace(buffer.String())
		if s == "" {
			status = 0
		} else if m := statusMatch.FindStringSubmatch(s); len(m) > 1 {
			status, _ = strconv.Atoi(m[1])
		} else {
			io.Copy(stdout, buffer)
		}
	}()

	return status, cmd.Run()
}
Пример #2
0
func buildAndRun() (int, error) {
	var status int

	bin, err := godog.Build()
	if err != nil {
		return 1, err
	}
	defer os.Remove(bin)

	cmd := exec.Command(bin, os.Args[1:]...)
	cmd.Stdout = stdout
	cmd.Stderr = stderr
	cmd.Env = os.Environ()

	if err = cmd.Start(); err != nil {
		return status, err
	}

	if err = cmd.Wait(); err != nil {
		if exiterr, ok := err.(*exec.ExitError); ok {
			// The program has exited with an exit code != 0
			status = 1

			// This works on both Unix and Windows. Although package
			// syscall is generally platform dependent, WaitStatus is
			// defined for both Unix and Windows and in both cases has
			// an ExitStatus() method with the same signature.
			if st, ok := exiterr.Sys().(syscall.WaitStatus); ok {
				status = st.ExitStatus()
			}
			return status, nil
		}
		return status, err
	}
	return status, nil
}