STDIN Pipe判定

https://github.com/monochromegane/the_platinum_searcher/pull/85


Windowsでは, STDINが pipeでないと, os.Stdin.Stat()がエラーを返すという
ことを知らずに, Emacsで the platinum searcherを動かすための PRをしたところ,
Windowsで動かなくなってしまい, その原因を調べました.


具体的には os.Stdin.Stat()の挙動は以下のようになります

 % some_command arg1        # os.Stdin.Stat() エラー
 % some_command arg1 < Nul  # os.Stdin.Stat() エラー
 % some_command < file      # os.Stdin.Stat() エラーなし
 % other_command | some_command # os.Stdin.Stat() エラーなし


Windowsを考慮すると Stdin PIPE判定は以下のように書く必要があります.

package main

import (
	"fmt"
	"os"
	"runtime"
)

func main() {
	fi, err := os.Stdin.Stat()
	if runtime.GOOS == "windows" {
		// error occurs if STDIN is not pipe
		// Ex: $ some_command arg1
		//     $ some_command < Nul
		if err == nil {
			fmt.Println("Stdin is pipe")
		} else {
			fmt.Println("Stdin is not pipe")
		}
	} else {
		if err != nil {
			fmt.Println(err)
			return
		}

		mode := fi.Mode()
		if mode&os.ModeNamedPipe != 0 || mode.IsRegular() {
			fmt.Println("Stdin is pipe")
		} else {
			fmt.Println("Stdin is not pipe")
		}
	}
}


これで期待通り動作しましたが, もっと良い書き方などありましたら教えていただければと思います.