/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Single-file source-code for gobble. To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath gobble.go */ package main import ( "bytes" "io" "os" "os/exec" ) const info = ` gobble [command...] [args...] Read all bytes from standard input, and only then emit them out, or run the command given, sending those bytes to its standard input. ` func main() { if len(os.Args) > 1 { switch os.Args[1] { case `-h`, `--h`, `-help`, `--help`: os.Stderr.WriteString(info[1:]) return } } // wait until the last byte is read input, err := io.ReadAll(os.Stdin) if err != nil { showError(err) os.Exit(1) } // emit data just read and quit, if no command was given if len(os.Args) == 1 { os.Stdout.Write(input) return } // run a command, using the data just read for its stdin name := os.Args[1] args := os.Args[2:] cmd := exec.Command(name, args...) cmd.Stdin = bytes.NewReader(input) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Start(); err != nil { showError(err) } if err := cmd.Wait(); err != nil { showError(err) } os.Exit(cmd.ProcessState.ExitCode()) } // showError gives a consistent style/look to any of the app's own errors func showError(err error) { os.Stderr.WriteString("\x1b[31m") os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\x1b[0m\n") }