本文整理汇总了Golang中os/exec.Cmd.StdinPipe方法的典型用法代码示例。如果您正苦于以下问题:Golang Cmd.StdinPipe方法的具体用法?Golang Cmd.StdinPipe怎么用?Golang Cmd.StdinPipe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os/exec.Cmd
的用法示例。
在下文中一共展示了Cmd.StdinPipe方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: runWithPty
func (sess *session) runWithPty(command *exec.Cmd) error {
logger := sess.logger.Session("run")
command.Stdout = sess.channel
command.Stderr = sess.channel.Stderr()
stdin, err := command.StdinPipe()
if err != nil {
return err
}
go func() {
for {
mybuffer := make([]byte, 255)
n, err := sess.channel.Read(mybuffer)
if err == nil {
inp := mybuffer[0:n]
logger.Info("stdin", lager.Data{"buffer": inp})
stdin.Write(inp)
if string(inp) == "\r" {
stdin.Write([]byte("\r\n"))
}
}
}
}()
return sess.runner.Start(command)
}
示例2: preprocessFile
func (c *ClientServer) preprocessFile(cmd *exec.Cmd, f io.Reader) ([]byte, error) {
if stdout, err := cmd.StdoutPipe(); err == nil {
if stdin, err := cmd.StdinPipe(); err == nil {
if stderr, err := cmd.StderrPipe(); err == nil {
if err := cmd.Start(); err == nil {
if b, err := ioutil.ReadAll(f); err == nil {
stdin.Write(b)
stdin.Close()
out, _ := ioutil.ReadAll(stdout)
stde, _ := ioutil.ReadAll(stderr)
if e := cmd.Wait(); e == nil {
return out, nil
} else {
return stde, fmt.Errorf("Preprocessor failed.")
}
} else {
return nil, err
}
} else {
return nil, err
}
} else {
return nil, err
}
} else {
return nil, err
}
} else {
return nil, err
}
}
示例3: Publish
// Publish prints the result of a check to stdout.
func (p *ExecPublisher) Publish(result *CheckResult) error {
var cmd *exec.Cmd
if len(p.cmd) > 1 {
cmd = exec.Command(p.cmd[0], p.cmd[1:]...)
} else {
cmd = exec.Command(p.cmd[0])
}
var b bytes.Buffer
cmd.Stdout = &b
cmd.Stderr = &b
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
if err = cmd.Start(); err != nil {
p.log.Error("Exec failed", "error", err)
return err
}
if err = p.stdinTemplate.Execute(stdin, result); err != nil {
p.log.Error("Failed to write template data to stdin", "error", err)
return err
}
stdin.Close()
if err = cmd.Wait(); err != nil {
p.log.Error("Exec failed", "error", err, "output", b.String())
return err
}
return nil
}
示例4: rsyncWebsocket
func rsyncWebsocket(cmd *exec.Cmd, conn *websocket.Conn) error {
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
shared.WebsocketMirror(conn, stdin, stdout)
data, err2 := ioutil.ReadAll(stderr)
if err2 != nil {
shared.Debugf("error reading rsync stderr: %s", err2)
return err2
}
shared.Debugf("Stderr from rsync: %s", data)
err = cmd.Wait()
if err != nil {
shared.Debugf("rsync recv error %s: %s", err, string(data))
}
return err
}
示例5: execCommand
func execCommand(_result map[string]int, _cmd []string) {
var r ResultSlice
for k, v := range _result {
r.Results = append(r.Results, Result{Host: k, Count: v})
}
b, err := json.Marshal(r)
HandleError(err)
var cmd *exec.Cmd
if len(_cmd) == 0 {
cmd = exec.Command(_cmd[0])
} else {
cmd = exec.Command(_cmd[0], _cmd[1:]...)
}
stdin, err := cmd.StdinPipe()
HandleError(err)
io.WriteString(stdin, string(b))
stdin.Close()
out, err := cmd.CombinedOutput()
HandleError(err)
fmt.Println(string(out))
}
示例6: attachCmd
func attachCmd(cmd *exec.Cmd, stdout, stderr io.Writer, stdin io.Reader) (*sync.WaitGroup, error) {
var wg sync.WaitGroup
wg.Add(2)
stdinIn, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
stdoutOut, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderrOut, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
go func() {
io.Copy(stdinIn, stdin)
stdinIn.Close()
}()
go func() {
io.Copy(stdout, stdoutOut)
wg.Done()
}()
go func() {
io.Copy(stderr, stderrOut)
wg.Done()
}()
return &wg, nil
}
示例7: Start
func (c *Communicator) Start(remote *packer.RemoteCmd) error {
var cmd *exec.Cmd
if c.Config.Pty {
cmd = exec.Command("docker", "exec", "-i", "-t", c.ContainerId, "/bin/sh", "-c", fmt.Sprintf("(%s)", remote.Command))
} else {
cmd = exec.Command("docker", "exec", "-i", c.ContainerId, "/bin/sh", "-c", fmt.Sprintf("(%s)", remote.Command))
}
var (
stdin_w io.WriteCloser
err error
)
stdin_w, err = cmd.StdinPipe()
if err != nil {
return err
}
stderr_r, err := cmd.StderrPipe()
if err != nil {
return err
}
stdout_r, err := cmd.StdoutPipe()
if err != nil {
return err
}
// Run the actual command in a goroutine so that Start doesn't block
go c.run(cmd, remote, stdin_w, stdout_r, stderr_r)
return nil
}
示例8: start
func start(c *exec.Cmd) (io.ReadCloser, io.WriteCloser, error) {
c.SysProcAttr = &syscall.SysProcAttr{}
//c.SysProcAttr.CreationFlags = 16 // CREATE_NEW_CONSOLE
stdin, err := c.StdinPipe()
if err != nil {
return nil, nil, err
}
stdout, err := c.StdoutPipe()
if err != nil {
return nil, nil, err
}
stderr, err := c.StderrPipe()
if err != nil {
return nil, nil, err
}
t := &term{}
t.cmd = c
t.stderr = stderr
t.stdout = stdout
t.stdin = stdin
err = t.start()
if err != nil {
return nil, nil, err
}
return t, t, nil
}
示例9: runPlayer
func runPlayer(command string, msgPath string) (*exec.Cmd, io.WriteCloser, io.ReadCloser, error) {
if argv := strings.Fields(command); len(argv) == 0 {
return nil, nil, nil, os.ErrInvalid
} else if name, err := exec.LookPath(argv[0]); err != nil {
return nil, nil, nil, err
} else if dir, err := os.Getwd(); err != nil {
return nil, nil, nil, err
} else {
cmd := exec.Cmd{Path: name, Args: argv, Dir: dir}
if stdin, err := cmd.StdinPipe(); err != nil {
return nil, nil, nil, err
} else if stdout, err := cmd.StdoutPipe(); err != nil {
return nil, nil, nil, err
} else {
if msgPath == "-" {
cmd.Stderr = os.Stderr
} else if msgPath != "" {
if w, err := os.Create(msgPath); err != nil {
// Connect to stderr instead
fmt.Fprintln(os.Stderr, err)
cmd.Stderr = os.Stderr
} else {
cmd.Stderr = w
}
}
if err := cmd.Start(); err != nil {
return nil, nil, nil, err
}
return &cmd, stdin, stdout, nil
}
}
}
示例10: attachCmd
func attachCmd(cmd *exec.Cmd, stdout, stderr io.Writer, stdin io.Reader) chan error {
errCh := make(chan error)
stdinIn, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
stdoutOut, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
stderrOut, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
go func() {
_, e := io.Copy(stdinIn, stdin)
errCh <- e
}()
go func() {
_, e := io.Copy(stdout, stdoutOut)
errCh <- e
}()
go func() {
_, e := io.Copy(stderr, stderrOut)
errCh <- e
}()
return errCh
}
示例11: attachCmd
func attachCmd(cmd *exec.Cmd, ch ssh.Channel) (*sync.WaitGroup, error) {
var wg sync.WaitGroup
wg.Add(3)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
go func() {
defer wg.Done()
io.Copy(stdin, ch)
}()
go func() {
defer wg.Done()
io.Copy(ch.Stderr(), stderr)
}()
go func() {
defer wg.Done()
io.Copy(ch, stdout)
}()
return &wg, nil
}
示例12: WrapinMKV
func WrapinMKV(uuidpath string, audioin chan []byte, audio bool) {
var ffmpeg *exec.Cmd
if audio {
ffmpeg = exec.Command("ffmpeg", "-f", "mjpeg", "-i", uuidpath, "-f", "s32be", "-ac", "2", "-ar", "44100", "-i", "pipe:0", "-f", "matroska", "-codec", "copy", "pipe:1")
} else {
ffmpeg = exec.Command("ffmpeg", "-f", "mjpeg", "-i", uuidpath, "-f", "matroska", "-codec", "copy", "pipe:1")
}
ffmpegstdout, err := ffmpeg.StdoutPipe()
if err != nil {
log.Fatalf("Unable to setup pipes for ffmpeg (stdout)")
}
ffmpeg.Stderr = os.Stderr
audiofile, err := ffmpeg.StdinPipe()
go DumpChanToFile(audioin, audiofile)
ffmpeg.Start()
for {
_, err := io.Copy(os.Stdout, ffmpegstdout)
if err != nil {
log.Fatalf("unable to read to stdout: %s", err.Error())
}
}
}
示例13: wrapStdin
func wrapStdin(proc *exec.Cmd, stdin io.Reader, done chan bool) {
logger.Println("Wrapping stdin")
pipe, err := proc.StdinPipe()
fatal_if(err)
go inLoop(pipe, stdin, done)
}
示例14: sudo
func sudo(user string, cmdStr string, shell string, input *string) (*SudoResult, *JobberError) {
var cmd *exec.Cmd = exec.Command("su",
"--login", // login shell
"--shell", shell,
"--command", cmdStr,
user)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, &JobberError{"Failed to get pipe to stdout.", err}
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, &JobberError{"Failed to get pipe to stderr.", err}
}
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, &JobberError{"Failed to get pipe to stdin.", err}
}
// start cmd
if err := cmd.Start(); err != nil {
return nil, &JobberError{"Failed to execute command \"" + cmdStr + "\".", err}
}
if input != nil {
// write input
stdin.Write([]byte(*input))
}
stdin.Close()
// read output
stdoutBytes, err := ioutil.ReadAll(stdout)
if err != nil {
return nil, &JobberError{"Failed to read stdout.", err}
}
stdoutStr := string(stdoutBytes)
stderrBytes, err := ioutil.ReadAll(stderr)
if err != nil {
return nil, &JobberError{"Failed to read stderr.", err}
}
stderrStr := string(stderrBytes)
// finish execution
err = cmd.Wait()
if err != nil {
_, flag := err.(*exec.ExitError)
if !flag {
return nil, &JobberError{"Failed to execute command \"" + cmdStr + "\".", err}
}
}
// return result
res := &SudoResult{}
res.Stdout = stdoutStr
res.Stderr = stderrStr
res.Succeeded = (err == nil)
return res, nil
}
示例15: RunPipe
func RunPipe(src, dst *exec.Cmd) (int64, error) {
if src.Stdout != nil {
return 0, fmt.Errorf("RunPipe: stdout already set on source")
}
if dst.Stdin != nil {
return 0, fmt.Errorf("RunPipe: stdin already set on dest")
}
log.Spamf("RunPipe: src=%q dst=%q", src.Path, dst.Path)
pr, err := src.StdoutPipe()
if err != nil {
return 0, err
}
pw, err := dst.StdinPipe()
if err != nil {
return 0, err
}
log.Spam("RunPipe: starting src")
err = src.Start()
if err != nil {
log.Spam("RunPipe: error starting src: ", err)
return 0, err
}
log.Spam("RunPipe: starting dst")
err = dst.Start()
if err != nil {
log.Spam("RunPipe: error starting dst: ", err)
log.Spam("RunPipe: waiting for src to die")
pr.Close()
src.Wait()
return 0, err
}
log.Spam("RunPipe: copying data")
copied, copyErr := io.Copy(pw, pr)
if copyErr == io.EOF {
copyErr = nil // EOF isn't really an error in this case
}
log.Spamf("RunPipe: copied %d bytes, err: %v", copied, copyErr)
// close all pipes and let everything die
log.Spam("RunPipe: waiting for all children to die")
closeErr1, closeErr2 := pr.Close(), pw.Close()
waitErr1, waitErr2 := src.Wait(), dst.Wait()
// and finally, figure out resulting error code
errs := []error{copyErr, waitErr1, waitErr2, closeErr1, closeErr2}
for _, e := range errs {
if e != nil {
return copied, e
}
}
return copied, nil
}