stdin doesnt work YET

This commit is contained in:
Hedy Li 2021-07-09 17:54:21 +08:00
parent b954d02f5d
commit f5b58ce4ff
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
1 changed files with 20 additions and 4 deletions

View File

@ -133,10 +133,26 @@ func main() {
break
}
// arg must be -f
inputBytes, err := ioutil.ReadFile(args[i+1])
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading file")
os.Exit(1)
file := args[i+1]
var inputBytes []byte
var err error
if file == "-" {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
fmt.Println("nothing in stdin")
os.Exit(1)
}
inputBytes, err = ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading stdin")
os.Exit(1)
}
} else {
inputBytes, err = ioutil.ReadFile(args[i+1])
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading file")
os.Exit(1)
}
}
input = string(inputBytes)
break