Better handling of input file errors (#187)

* Handle error when no input is provided

* Improve error when input file can't be read
sleep-stdin-bug
Alexandre GUIOT--VALENTIN 2 years ago committed by GitHub
parent a68c947949
commit aa0964a12e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,6 +11,7 @@ import (
"golang.org/x/term" "golang.org/x/term"
"os" "os"
"path" "path"
"io/fs"
"runtime/pprof" "runtime/pprof"
"strings" "strings"
) )
@ -50,7 +51,13 @@ func main() {
filePath = os.Args[1] filePath = os.Args[1]
f, err := os.Open(os.Args[1]) f, err := os.Open(os.Args[1])
if err != nil { if err != nil {
panic(err) switch err.(type) {
case *fs.PathError:
fmt.Println(err)
os.Exit(1)
default:
panic(err)
}
} }
dec = json.NewDecoder(f) dec = json.NewDecoder(f)
args = os.Args[2:] args = os.Args[2:]
@ -59,6 +66,10 @@ func main() {
dec = json.NewDecoder(os.Stdin) dec = json.NewDecoder(os.Stdin)
args = os.Args[1:] args = os.Args[1:]
} }
if dec == nil {
fmt.Println("No input provided. Usage: `fx data.json` or `curl ... | fx`")
os.Exit(1)
}
dec.UseNumber() dec.UseNumber()
jsonObject, err := parse(dec) jsonObject, err := parse(dec)
if err != nil { if err != nil {

Loading…
Cancel
Save