requires stdin content on certain commands

Taken from:

http://stackoverflow.com/questions/22744443/check-if-there-is-something-
to-read-on-stdin-in-golang
This commit is contained in:
Rick Olson 2015-04-24 14:24:32 -06:00
parent 2e6bb2a045
commit 596c5f6560
6 changed files with 14 additions and 1 deletions

@ -16,6 +16,7 @@ var (
)
func cleanCommand(cmd *cobra.Command, args []string) {
requireStdin("This command should be run by the Git 'clean' filter")
lfs.InstallHooks(false)
var filename string

@ -115,6 +115,8 @@ func pointerReader() (io.ReadCloser, error) {
return os.Open(pointerCompare)
}
requireStdin("The --stdin flag expects a pointer file from STDIN.")
return os.Stdin, nil
}

@ -62,6 +62,7 @@ func pushCommand(cmd *cobra.Command, args []string) {
lfs.Config.CurrentRemote = args[0]
if useStdin {
requireStdin("Run this command from the Git pre-push hook, or leave the --stdin flag off.")
refsData, err := ioutil.ReadAll(os.Stdin)
if err != nil {
Panic(err, "Error reading refs on stdin")

@ -20,6 +20,7 @@ var (
)
func smudgeCommand(cmd *cobra.Command, args []string) {
requireStdin("This command should be run by the Git 'smudge' filter")
lfs.InstallHooks(false)
b := &bytes.Buffer{}

@ -92,6 +92,14 @@ func PipeCommand(name string, args ...string) error {
return cmd.Run()
}
func requireStdin(msg string) {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
Error("Cannot read from STDIN. %s", msg)
os.Exit(1)
}
}
func handlePanic(err error) string {
if err == nil {
return ""

@ -79,7 +79,7 @@ func TestPointerWithInvalidCompareStdin(t *testing.T) {
defer repo.Test()
cmd := repo.Command("pointer", "--stdin")
cmd.Output = "Pointer from STDIN\n\nEOF"
cmd.Output = "Cannot read from STDIN. The --stdin flag expects a pointer file from STDIN."
cmd.Unsuccessful = true
}