From 0b8b4614081c1ba64a426deb94f5f12eafebf572 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Thu, 8 Nov 2018 09:17:38 +0800 Subject: [PATCH] abandon terminal interactive mod check stdin is terminal or pipe. if read from terminal interactive printf help message. --- cmd/soar/soar.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cmd/soar/soar.go b/cmd/soar/soar.go index 6ea7a05..f1afe8c 100644 --- a/cmd/soar/soar.go +++ b/cmd/soar/soar.go @@ -104,6 +104,19 @@ func main() { // 读入待优化 SQL ,当配置文件或命令行参数未指定 SQL 时从管道读取 if common.Config.Query == "" { + // check stdin is pipe or terminal + // https://stackoverflow.com/questions/22744443/check-if-there-is-something-to-read-on-stdin-in-golang + stat, err := os.Stdin.Stat() + if stat == nil { + common.Log.Critical("os.Stdin.Stat Error: %v", err) + os.Exit(1) + } + if (stat.Mode() & os.ModeCharDevice) != 0 { + // stdin is from a terminal + fmt.Println("Args format error, use --help see how to use it!") + os.Exit(1) + } + // read from pipe var data []byte data, err = ioutil.ReadAll(os.Stdin) if err != nil { -- GitLab