From 0bd254c2ebff31bc1793fff2de102e8ad9dbe050 Mon Sep 17 00:00:00 2001 From: Grant Murphy Date: Fri, 22 Jul 2016 11:05:05 -0700 Subject: [PATCH] Check input files and handle panic condition --- tools.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools.go b/tools.go index 315339b..d221e59 100644 --- a/tools.go +++ b/tools.go @@ -19,6 +19,7 @@ import ( "go/ast" "go/parser" "go/token" + "os" "strings" ) @@ -64,11 +65,23 @@ func (u *utilities) run(args ...string) { func dumpAst(files ...string) { for _, arg := range files { + // Ensure file exists and not a directory + st, e := os.Stat(arg) + if e != nil { + fmt.Fprintf(os.Stderr, "Skipping: %s - %s\n", arg, e) + continue + } + if st.IsDir() { + fmt.Fprintf(os.Stderr, "Skipping: %s - directory\n", arg) + continue + } + // Create the AST by parsing src. fset := token.NewFileSet() // positions are relative to fset f, err := parser.ParseFile(fset, arg, nil, 0) if err != nil { - panic(err) + fmt.Fprintf(os.Stderr, "Unable to parse file %s\n", err) + continue } // Print the AST.