Skip to content

Instantly share code, notes, and snippets.

@jesusvazquez
Created March 4, 2020 21:02
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 jesusvazquez/326530478f173204d3760a2e22f0dbd9 to your computer and use it in GitHub Desktop.
Save jesusvazquez/326530478f173204d3760a2e22f0dbd9 to your computer and use it in GitHub Desktop.
Know if a go program is receiving stdin input or not
package main
import (
"io/ioutil"
"os"
"github.com/sirupsen/logrus"
)
func main() {
fi, err := os.Stdin.Stat()
if err != nil {
logrus.Error("error reading from stdin")
os.Exit(1)
panic(err)
}
logrus.Infof("File Mode %s", fi.Mode())
logrus.Infof("Os Mode Named Pipe %s", os.ModeNamedPipe)
logrus.Infof("Operation %s", fi.Mode()&os.ModeNamedPipe)
if fi.Mode()&os.ModeNamedPipe == 0 {
logrus.Infof("No STDIN Input")
} else {
var data []byte
data, err = ioutil.ReadAll(os.Stdin)
if err != nil {
logrus.Errorf("error reading from file %v", err)
}
logrus.Infof("There is STDIN '%s'", data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment