Пример #1
0
func RunWithEnvAndWd(command string, args []string, env []string, wd string) (proc *exec.Cmd, err os.Error) {
	//log.Println(command, args)
	//hho := exec.PassThrough
	args = prepend(args, command)
	env = mergeEnv(os.Environ(), env)

	binpath, err := findCmd(findEnv(env, "PATH"), command)
	if err != nil {
		return nil, err
	}

	cmd := new(exec.Cmd)
	cmd.Path = binpath
	cmd.Args = args
	cmd.Env = env
	cmd.Dir = wd
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout

	err = cmd.Start()
	if err != nil {
		log.Print("Error running command ", command, ": ", err, "\n")
		return nil, err
	}
	return cmd, nil
}
Пример #2
0
func RunExternalAndStdout(cmd, wd string, argv []string) (stdout []byte, err os.Error) {
	argv = SplitArgs(argv)
	var c *exec.Cmd
	if len(argv) > 0 {
		c = exec.Command(cmd, argv...)
	} else {
		c = exec.Command(cmd)
	}
	c.Dir = wd
	c.Env = os.Environ()

	bStdout := bytes.NewBuffer(nil)
	bStderr := bytes.NewBuffer(nil)
	c.Stdout = bStdout
	c.Stderr = bStderr

	err = c.Run()

	if wmsg, ok := err.(*os.Waitmsg); ok {
		if wmsg.ExitStatus() != 0 {
			err = os.NewError(fmt.Sprintf("command: %s %v\n%s\n", cmd, argv, bStderr.String()))
		} else {
			err = nil
		}
	} else {
		stdout = bStdout.Bytes()
	}
	return
}
Пример #3
0
Файл: handy.go Проект: ssrl/mgd
func StdExecve(argv []string, stopOnTrouble bool) bool {

	var err os.Error
	var cmd *exec.Cmd

	switch len(argv) {
	case 0:
		if stopOnTrouble {
			log.Fatalf("[ERROR] len(argv) == 0\n")
		}
		return false
	case 1:
		cmd = exec.Command(argv[0])
	default:
		cmd = exec.Command(argv[0], argv[1:]...)
	}

	// pass-through
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin

	err = cmd.Start()

	if err != nil {
		if stopOnTrouble {
			log.Fatalf("[ERROR] %s\n", err)
		} else {
			log.Printf("[ERROR] %s\n", err)
			return false
		}
	}

	err = cmd.Wait()

	if err != nil {
		if stopOnTrouble {
			log.Fatalf("[ERROR] %s\n", err)
		} else {
			log.Printf("[ERROR] %s\n", err)
			return false
		}
	}

	return true
}
Пример #4
0
/*
Set up a command to use os standard in/out/err
*/
func set_exec(c *exec.Cmd) {
	c.Stdout = os.Stdout
	c.Stdin = os.Stdin
	c.Stderr = os.Stderr
}