Skip to content

Instantly share code, notes, and snippets.

@aereal
Created March 6, 2022 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aereal/f2efcd0d78b3b1f2f103a7b92dcba361 to your computer and use it in GitHub Desktop.
Save aereal/f2efcd0d78b3b1f2f103a7b92dcba361 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"runtime"
)
func command(path string, args ...string) *exec.Cmd {
return exec.Command(path, args...)
}
func main() {
l := log.New(os.Stderr, "", log.Ldate|log.Lmicroseconds)
l.Printf("start w/%s", runtime.Version())
input := command("echo", "ababa")
input.Stderr = os.Stderr
filterOut := new(bytes.Buffer)
filter := command("sed", "s/a/A/g")
filter.Stdout = filterOut
var err error
filter.Stdin, err = input.StdoutPipe()
if err != nil {
fmt.Printf("! cannot open stdout pipe: %+v\n", err)
os.Exit(1)
}
l.Printf("%s start", input.Path)
if err := input.Start(); err != nil {
fmt.Printf("! cannot start %s: %+v\n", input.Path, err)
os.Exit(1)
}
l.Printf("%s start", filter.Path)
if err := filter.Start(); err != nil {
fmt.Printf("! cannot start %s: %+v\n", filter.Path, err)
os.Exit(1)
}
l.Printf("waiting for %s", input.Path)
if err := input.Wait(); err != nil {
fmt.Printf("! fail %s: %+v\n", input.Path, err)
os.Exit(1)
}
l.Printf("waiting for %s", filter.Path)
if err := filter.Wait(); err != nil {
fmt.Printf("! fail %s: %+v\n", filter.Path, err)
os.Exit(1)
}
fmt.Printf("filter output: %s\n", filterOut.String())
}
# from https://go.dev/play/p/mf0DsQHVAJN
2009/11/10 23:00:00.000000 start w/Go go1.17.8
2009/11/10 23:00:00.000000 /bin/echo start
2009/11/10 23:00:00.000000 /bin/sed start
2009/11/10 23:00:00.000000 waiting for /bin/echo
2009/11/10 23:00:00.000000 waiting for /bin/sed
filter output: AbAbA
Program exited.
[2022-03-06 22:31:57] ✘╹◡╹✘ < go build -o a ./tmp
[2022-03-06 22:32:21] ✘╹◡╹✘ < ./a
2022/03/06 22:32:33.775941 start w/go1.17.7
2022/03/06 22:32:33.776988 /usr/local/opt/coreutils/libexec/gnubin/echo start
2022/03/06 22:32:44.449082 /usr/bin/sed start
2022/03/06 22:32:55.098631 waiting for /usr/local/opt/coreutils/libexec/gnubin/echo
2022/03/06 22:32:55.098678 waiting for /usr/bin/sed
filter output: AbAbA
[2022-03-06 22:34:19] ✘╹◡╹✘ < uname -s -r -v -m -p
Darwin 19.6.0 Darwin Kernel Version 19.6.0: Tue Oct 12 18:34:05 PDT 2021; root:xnu-6153.141.43~1/RELEASE_X86_64 x86_64 i386
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment