Skip to content

Commit

Permalink
abandon terminal interactive mod
Browse files Browse the repository at this point in the history
  check stdin is terminal or pipe.
  if read from terminal interactive printf help message.
  • Loading branch information
martianzhang committed Nov 8, 2018
1 parent ee770b4 commit 0b8b461
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/soar/soar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0b8b461

Please sign in to comment.