Compare commits

...

2 Commits

Author SHA1 Message Date
b1ek c777bd25b0
treat '-' input file as stdin 2024-06-03 09:06:03 +10:00
b1ek 4842b4ad52
use a different stdin read method 2024-06-03 09:04:47 +10:00
1 changed files with 8 additions and 9 deletions

17
main.go
View File

@ -1,8 +1,8 @@
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
@ -29,7 +29,7 @@ func main() {
}
var file []byte
if args.File != "" {
if (args.File != "") && (args.File != "-") {
f, err := os.ReadFile(args.File)
if err != nil {
if os.IsNotExist(err) {
@ -40,14 +40,13 @@ func main() {
}
file = f
} else {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
file = scanner.Bytes()
}
data, err := io.ReadAll(os.Stdin)
if scanner.Err() != nil {
panic(scanner.Err())
if err != nil {
fmt.Printf("Coudln't read from stdin: %s", err.Error())
os.Exit(1)
}
file = data
}
code := string(file)
@ -70,7 +69,7 @@ func main() {
os.Exit(2)
}
gen := shebang + "\n\n" + gencode(found) + "\n" + strings.Join(codelines[1:], "\n")
gen := shebang + "\n\n" + gencode(found) + "\n\n" + strings.Join(codelines[1:], "\n")
if args.Outfile == "" {
fmt.Printf("%s", gen)