示例#1
0
func (gcmd *command) toExecCmd() (cmd *exec.Cmd, err error) {
	cmd = exec.Command(gcmd.executable, gcmd.argv...)
	if gcmd.wd != "" {
		cmd.Dir = gcmd.wd
	}

	cmd.Env = effectiveEnv(gcmd.env)
	cmd.Stdin = os.Stdin

	if gcmd.captureOutput {
		outWrapper := newFileWrapper(os.Stdout, &gcmd.recorder, "")
		errWrapper := newFileWrapper(os.Stderr, &gcmd.recorder, ansi.ColorCode("red+b"))
		cmd.Stdout = outWrapper
		cmd.Stderr = errWrapper
	} else {
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
	}

	if verbose {
		if Env != "" {
			util.Debug("#", "Env: %s\n", Env)
		}
		util.Debug("#", "%s\n", gcmd.commandstr)
	}

	return cmd, nil
}
示例#2
0
func newFileWrapper(file *os.File, recorder *bytes.Buffer, color string) *fileWrapper {
	streamer := &fileWrapper{
		file:       file,
		buf:        bytes.NewBuffer([]byte("")),
		recorder:   recorder,
		colorReset: ansi.ColorCode("reset"),
		colorStart: color,
	}

	return streamer
}