当前位置: 首页>>代码示例>>Golang>>正文


Golang Cmd.Stdout方法代码示例

本文整理汇总了Golang中os/exec.Cmd.Stdout方法的典型用法代码示例。如果您正苦于以下问题:Golang Cmd.Stdout方法的具体用法?Golang Cmd.Stdout怎么用?Golang Cmd.Stdout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在os/exec.Cmd的用法示例。


在下文中一共展示了Cmd.Stdout方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ExecBin

func (w *Worker) ExecBin(binPath string, args []string, maxRunTime int64) (string, error) {
	var cmd *exec.Cmd
	var stdout bytes.Buffer
	var stderr bytes.Buffer
	var err error

	if len(args) == 0 {
		cmd = exec.Command(binPath)
	} else {
		cmd = exec.Command(binPath, args...)
	}

	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	cmd.Start() // attention!

	err, _ = w.CmdRunWithTimeout(cmd,
		time.Duration(maxRunTime)*time.Second,
	)
	if err != nil {
		return "", err
	}
	if len(stderr.String()) != 0 {
		errMsg := strings.TrimRight(stderr.String(), "\n")
		return "", errors.NewError(errMsg)
	}

	return strings.TrimRight(stdout.String(), "\n"), nil
}
开发者ID:jameswei,项目名称:kingtask,代码行数:29,代码来源:worker.go

示例2: buildCmd

func buildCmd(cmd Cmd) *Result {
	var execCmd *exec.Cmd
	switch len(cmd.Command) {
	case 1:
		execCmd = exec.Command(cmd.Command[0])
	default:
		execCmd = exec.Command(cmd.Command[0], cmd.Command[1:]...)
	}
	outBuffer := new(lockedBuffer)
	errBuffer := new(lockedBuffer)

	execCmd.Stdin = cmd.Stdin
	execCmd.Dir = cmd.Dir
	execCmd.Env = cmd.Env
	if cmd.Stdout != nil {
		execCmd.Stdout = io.MultiWriter(outBuffer, cmd.Stdout)
	} else {
		execCmd.Stdout = outBuffer
	}
	execCmd.Stderr = errBuffer
	return &Result{
		Cmd:       execCmd,
		outBuffer: outBuffer,
		errBuffer: errBuffer,
	}
}
开发者ID:msabansal,项目名称:docker,代码行数:26,代码来源:command.go

示例3: runCmd

func runCmd(cmd *exec.Cmd) error {
	if buildX {
		dir := ""
		if cmd.Dir != "" {
			dir = "PWD=" + cmd.Dir + " "
		}
		env := strings.Join(cmd.Env, " ")
		if env != "" {
			env += " "
		}
		printcmd("%s%s%s", dir, env, strings.Join(cmd.Args, " "))
	}

	buf := new(bytes.Buffer)
	buf.WriteByte('\n')
	if buildV {
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
	} else {
		cmd.Stdout = buf
		cmd.Stderr = buf
	}

	if !buildN {
		cmd.Env = environ(cmd.Env)
		if err := cmd.Run(); err != nil {
			return fmt.Errorf("%s failed: %v%s", strings.Join(cmd.Args, " "), err, buf)
		}
	}
	return nil
}
开发者ID:maul5,项目名称:mobile,代码行数:31,代码来源:init.go

示例4: CloneRepo

func (r *AssetRoot) CloneRepo() error {
	var cmd *exec.Cmd
	repoHash := md5.Sum([]byte(r.GitRepo))
	if err := os.MkdirAll(".gitrepos", os.ModePerm); err != nil {
		return err
	}
	cloneDir := filepath.Join(".gitrepos", fmt.Sprintf("%x", repoHash))
	r.Path = filepath.Join(cloneDir, r.Path)
	if _, err := os.Stat(cloneDir); err != nil {
		cmd = exec.Command("git", "clone", r.GitRepo, "--branch", r.GitBranch, cloneDir)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		if err := cmd.Run(); err != nil {
			return err
		}
	}
	if r.GitRef == "" {
		return nil
	}
	cmd = exec.Command("bash", "-c", strings.Join([]string{
		"cd", cloneDir, "&&", "git", "checkout", r.GitRef,
		"&&", "cd", "../../",
	}, " "))
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}
开发者ID:jvatic,项目名称:asset-matrix-go,代码行数:27,代码来源:asset-root.go

示例5: finishRunning

func finishRunning(stepName string, cmd *exec.Cmd) bool {
	log.Printf("Running: %v", stepName)
	stdout, stderr := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
	if *verbose {
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
	} else {
		cmd.Stdout = stdout
		cmd.Stderr = stderr
	}

	done := make(chan struct{})
	defer close(done)
	go func() {
		for {
			select {
			case <-done:
				return
			case s := <-signals:
				cmd.Process.Signal(s)
			}
		}
	}()

	if err := cmd.Run(); err != nil {
		log.Printf("Error running %v: %v", stepName, err)
		if !*verbose {
			fmt.Printf("stdout:\n------\n%v\n------\n", string(stdout.Bytes()))
			fmt.Printf("stderr:\n------\n%v\n------\n", string(stderr.Bytes()))
		}
		return false
	}
	return true
}
开发者ID:ericcapricorn,项目名称:kubernetes,代码行数:34,代码来源:e2e.go

示例6: AppendStdoutWriter

func AppendStdoutWriter(cmd *exec.Cmd, writer io.Writer) {
	if cmd.Stdout == nil {
		cmd.Stdout = writer
	} else {
		cmd.Stdout = io.MultiWriter(cmd.Stdout, writer)
	}
}
开发者ID:cretz,项目名称:systrument,代码行数:7,代码来源:shell.go

示例7: Build

func (r *AgRecipe) Build(ctx *types.BuildContext) error {
	log.Info("Building the_silver_searcher")
	srcdir := r.UnpackedDir(ctx, r.Info())

	var cmd *exec.Cmd

	// Run autotools
	cmd = exec.Command("autoreconf", "-i")
	cmd.Dir = srcdir
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		log.WithField("err", err).Error("Could not run autotools")
		return err
	}

	log.Infof("Running ./configure")
	cmd = exec.Command(
		"./configure",
		"--host="+ctx.CrossPrefix,
		"--build=i686",
		"PKG_CONFIG=/bin/true",
	)
	cmd.Dir = srcdir
	cmd.Env = ctx.Env.
		Append("CC", ctx.StaticFlags).
		Set("CFLAGS", "-fPIC "+ctx.StaticFlags).
		Set("PCRE_LIBS", ctx.DependencyEnv["pcre"]["LDFLAGS"]).
		Set("PCRE_CFLAGS", ctx.DependencyEnv["pcre"]["CFLAGS"]).
		Set("LZMA_LIBS", ctx.DependencyEnv["lzma"]["LDFLAGS"]).
		Set("LZMA_CFLAGS", ctx.DependencyEnv["lzma"]["CFLAGS"]).
		Set("ZLIB_LIBS", ctx.DependencyEnv["zlib"]["LDFLAGS"]).
		Set("ZLIB_CFLAGS", ctx.DependencyEnv["zlib"]["CFLAGS"]).
		AsSlice()
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stdout

	if err := cmd.Run(); err != nil {
		log.WithField("err", err).Error("Could not run configure")
		return err
	}

	cmd = exec.Command("make")
	cmd.Dir = srcdir
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stdout

	if err := cmd.Run(); err != nil {
		log.WithField("err", err).Error("Could not run make")
		return err
	}

	log.Info("Finished building the_silver_searcher")
	return nil
}
开发者ID:andrew-d,项目名称:sbuild,代码行数:55,代码来源:ag.go

示例8: spawn

func spawn(cmd *exec.Cmd, ttySpec *garden.TTYSpec, stdout io.Writer, stderr io.Writer) (process, io.WriteCloser, error) {
	var stdin io.WriteCloser
	var err error

	var processPty *os.File

	if ttySpec != nil {
		pty, tty, err := pty.Open()
		if err != nil {
			return nil, nil, err
		}

		// close our end of the tty after the process has spawned
		defer tty.Close()

		processPty = pty
		stdin = pty

		windowColumns := 80
		windowRows := 24
		if ttySpec.WindowSize != nil {
			windowColumns = ttySpec.WindowSize.Columns
			windowRows = ttySpec.WindowSize.Rows
		}

		ptyutil.SetWinSize(pty, windowColumns, windowRows)

		cmd.Stdin = tty
		cmd.Stdout = tty
		cmd.Stderr = tty

		go io.Copy(stdout, pty)
	} else {
		stdin, err = cmd.StdinPipe()
		if err != nil {
			return nil, nil, err
		}

		cmd.Stdout = stdout
		cmd.Stderr = stderr
	}

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

	return &groupProcess{
		process:    cmd.Process,
		processPty: processPty,
	}, stdin, nil
}
开发者ID:pcfdev-forks,项目名称:houdini,代码行数:52,代码来源:spawn.go

示例9: Build

func (r *PvRecipe) Build(ctx *types.BuildContext) error {
	log.Info("Building pv")
	srcdir := r.UnpackedDir(ctx, r.Info())

	var cmd *exec.Cmd

	// 1. Configure
	log.Infof("Running ./configure")
	cmd = exec.Command(
		"./configure",
		"--host="+ctx.CrossPrefix,
		"--build=i686",
	)
	cmd.Dir = srcdir
	cmd.Env = ctx.Env.
		Append("CC", ctx.StaticFlags).
		Set("CFLAGS", ctx.StaticFlags).
		AsSlice()
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stdout

	if err := cmd.Run(); err != nil {
		log.WithField("err", err).Error("Could not run configure")
		return err
	}

	// 2. Fix linker in Makefile
	cmd = exec.Command(
		"sed",
		"-i",
		fmt.Sprintf("/^CC =/a LD = %s", ctx.Env.Get("LD")),
		filepath.Join(srcdir, "Makefile"),
	)
	if err := cmd.Run(); err != nil {
		log.WithField("err", err).Error("Could not patch Makefile")
		return err
	}

	// 3. Run build
	cmd = exec.Command("make")
	cmd.Dir = srcdir
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stdout

	if err := cmd.Run(); err != nil {
		log.WithField("err", err).Error("Could not run make")
		return err
	}

	log.Info("Finished building pv")
	return nil
}
开发者ID:andrew-d,项目名称:sbuild,代码行数:52,代码来源:pv.go

示例10: cloneRepo

func cloneRepo(name, location string) error {
	src := "https://github.com/eris-ltd/" + name
	var c *exec.Cmd

	// if the .git directory exists within ~/.eris/services (or w/e)
	//   then pull rather than clone.
	if _, err := os.Stat(filepath.Join(location, ".git")); !os.IsNotExist(err) {
		logger.Debugf("The location exists. Attempting to pull instead.\n")
		if err := pullRepo(location); err != nil {
			return err
		} else {
			return nil
		}
	}

	// if the ~/.eris/services (or w/e) directory exists, but it does
	//   not have a .git directory (caught above), then init the dir
	//   and pull the repo.
	if _, err := os.Stat(location); !os.IsNotExist(err) {
		c = exec.Command("git", "init", location)
		c.Stdout = config.GlobalConfig.Writer
		c.Stderr = config.GlobalConfig.ErrorWriter
		if e2 := c.Run(); e2 != nil {
			return e2
		}

		c = exec.Command("git", "remote", "add", "origin", src)
		if e3 := c.Run(); e3 != nil {
			return e3
		}

		if err := pullRepo(location); err != nil {
			return err
		} else {
			return nil
		}

		// if no ~/.eris services (or w/e) directory exists, then it will
		//   simply clone in the directory.
	} else {
		c = exec.Command("git", "clone", src, location)
		c.Stdout = config.GlobalConfig.Writer
		c.Stderr = config.GlobalConfig.ErrorWriter
		if err := c.Run(); err != nil {
			return err
		}
	}

	return nil
}
开发者ID:kustomzone,项目名称:eris-cli,代码行数:50,代码来源:writers.go

示例11: tee

func (r *RealCommandRunner) tee(cmd *exec.Cmd) {
	if cmd.Stderr == nil {
		cmd.Stderr = os.Stderr
	} else if cmd.Stderr != nil {
		cmd.Stderr = io.MultiWriter(cmd.Stderr, os.Stderr)
	}

	if cmd.Stdout == nil {
		cmd.Stdout = os.Stderr

	} else if cmd.Stdout != nil {
		cmd.Stdout = io.MultiWriter(cmd.Stdout, os.Stderr)
	}
}
开发者ID:WIZARD-CXY,项目名称:golang-devops-stuff,代码行数:14,代码来源:command_runner.go

示例12: SetUp

func (I InputTester) SetUp(C *context, cmd *exec.Cmd) error {
	log.Printf("Testing input '%s'\n", prefix(I.Input, 20))
	cmd.Stdin = strings.NewReader(I.Input)
	state := C.State.(*InputTesterState)
	switch C.Mode() {
	case "model":
		cmd.Stdout = &state.modelOut
	case "accused":
		cmd.Stdout = &state.accusedOut
	default:
		return fmt.Errorf("Unknown mode '%s'\n", C.Mode())
	}
	return nil
}
开发者ID:pauek,项目名称:garzon.old,代码行数:14,代码来源:tester_input.go

示例13: RunCmd

// RunCmd runs c (which is assumed to be something short-lived, like a
// camput or camget command), capturing its stdout for return, and
// also capturing its stderr, just in the case of errors.
// If there's an error, the return error fully describes the command and
// all output.
func RunCmd(c *exec.Cmd) (output string, err error) {
	var stdout, stderr bytes.Buffer
	if testing.Verbose() {
		c.Stderr = io.MultiWriter(os.Stderr, &stderr)
		c.Stdout = io.MultiWriter(os.Stdout, &stdout)
	} else {
		c.Stderr = &stderr
		c.Stdout = &stdout
	}
	err = c.Run()
	if err != nil {
		return "", fmt.Errorf("Error running command %+v: Stdout:\n%s\nStderr:\n%s\n", c, stdout.String(), stderr.String())
	}
	return stdout.String(), nil
}
开发者ID:stevearm,项目名称:camlistore,代码行数:20,代码来源:world.go

示例14: install

func (a *AutoInstOptions) install() {
	sfx := ""
	if a.InstallSuffix != "" {
		sfx = a.InstallSuffix
	}
	osArchSfx := osArch + sfx
	if a.Env == nil {
		a.Env = map[string]string{}
	}

	roots := []string{}

	if p := a.Env["GOROOT"]; p != "" {
		roots = append(roots, filepath.Join(p, "pkg", osArchSfx))
	}

	for _, p := range pathList(a.Env["GOPATH"]) {
		roots = append(roots, filepath.Join(p, "pkg", osArchSfx))
	}

	if len(roots) == 0 {
		return
	}

	archiveOk := func(fn string) bool {
		for _, root := range roots {
			_, err := os.Stat(filepath.Join(root, fn))
			if err == nil {
				return true
			}
		}
		return false
	}

	el := envSlice(a.Env)
	installed := []string{}

	for path, fn := range a.imports() {
		if !archiveOk(fn) {
			var cmd *exec.Cmd
			if sfx == "" {
				cmd = exec.Command("go", "install", path)
			} else {
				cmd = exec.Command("go", "install", "-installsuffix", sfx, path)
			}
			cmd.Env = el
			cmd.Stderr = ioutil.Discard
			cmd.Stdout = ioutil.Discard
			cmd.Run()

			if archiveOk(fn) {
				installed = append(installed, path)
			}
		}
	}

	if len(installed) > 0 {
		postMessage("auto-installed: %v", strings.Join(installed, ", "))
	}
}
开发者ID:nuance,项目名称:GoSublime,代码行数:60,代码来源:g_autoinst.go

示例15: StopPgpool

func StopPgpool(w rest.ResponseWriter, r *rest.Request) {
	logit.Info.Println("StopPgpool called")
	response := StopPgpoolResponse{}
	req := StopPgpoolRequest{}
	err := r.DecodeJsonPayload(&req)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	var cmd *exec.Cmd
	cmd = exec.Command("stop-pgpool.sh", req.Path)
	var out bytes.Buffer
	var stderr bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &stderr
	err = cmd.Run()
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), 400)
		return
	}

	response.Output = out.String()
	response.Status = "OK"

	w.WriteJson(&response)
}
开发者ID:CrunchyData,项目名称:crunchy-postgresql-manager-openshift,代码行数:29,代码来源:pgpool.go


注:本文中的os/exec.Cmd.Stdout方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。