Hello Gophers,
I noticed some unexpected behavior when feeding a buffer to an exec.Command
.
There is a tool to create image thumbnails, called “vipsthumbnail”, that I am using in a pipeline architecture like this:
vipsthumbnail stdin -o thumbnail.webp < original.jpeg
It creates the thumbnail as expected.
The go equivalent also works the same way:
original, err := os.Open("original.jpeg")
if err != nil {
log.Fatal(err)
}
cmd := exec.Command("vipsthumbnail", "stdin", "-o", "thumbnail.webp")
cmd.Stdin = original
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
However, using a buffer does not work:
original, err := os.ReadFile("original.jpeg")
if err != nil {
log.Fatal(err)
}
cmd := exec.Command("vipsthumbnail", "stdin", "-o", "thumbnail.webp")
cmd.Stdin = bytes.NewReader(original)
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
Using a buffer, the vipsthumbnail tool fails with some generic “unable to read from stdin” message.
From the Go docs we get:
If Stdin is an *os.File, the process’s standard input is connected directly to that file.
Otherwise, during the execution of the command a separate goroutine reads from Stdin and delivers that data to the command over a pipe.
Is this an implementation error in the vipsthumbnail software not beeing able to handle pipes properly? How does it make a difference for that software at all? Is there anything I can do in my Go code?
Thanks for all the help!