MCPcopy
hub / github.com/bitfield/script / Exec

Method Exec

script.go:429–454  ·  view source on GitHub ↗

Exec runs cmdLine as an external command, sending it the contents of the pipe as input, and produces the command's standard output (see below for error output). The effect of this is to filter the contents of the pipe through the external command. # Environment The command inherits the current pro

(cmdLine string)

Source from the content-addressed store, hash-verified

427// pipe, along with its standard output. However, the standard error text can
428// instead be redirected to a supplied writer, using [Pipe.WithStderr].
429func (p *Pipe) Exec(cmdLine string) *Pipe {
430 return p.Filter(func(r io.Reader, w io.Writer) error {
431 args, err := shell.Fields(cmdLine, nil)
432 if err != nil {
433 return err
434 }
435 cmd := exec.Command(args[0], args[1:]...)
436 cmd.Stdin = r
437 cmd.Stdout = w
438 cmd.Stderr = w
439 pipeStderr := p.stdErr()
440 if pipeStderr != nil {
441 cmd.Stderr = pipeStderr
442 }
443 pipeEnv := p.environment()
444 if pipeEnv != nil {
445 cmd.Env = pipeEnv
446 }
447 err = cmd.Start()
448 if err != nil {
449 fmt.Fprintln(cmd.Stderr, err)
450 return err
451 }
452 return cmd.Wait()
453 })
454}
455
456// ExecForEach renders cmdLine as a Go template for each line of input, running
457// the resulting command, and produces the combined output of all these

Calls 4

FilterMethod · 0.95
stdErrMethod · 0.95
environmentMethod · 0.95
WaitMethod · 0.80