@@ -3,6 +3,7 @@ package main
33import  (
44	"flag" 
55	"fmt" 
6+ 	"log" 
67	"os" 
78	"path/filepath" 
89	"privado.ai/goastgen/goastgen" 
@@ -20,7 +21,9 @@ func processRequest(out string, inputPath string) {
2021	if  strings .HasSuffix (inputPath , ".go" ) {
2122		fileInfo , err  :=  os .Stat (inputPath )
2223		if  err  !=  nil  {
23- 			fmt .Println ("Failed to get file info:" , err )
24+ 			log .SetPrefix ("[ERROR]" )
25+ 			log .Println ("Failed to get file info:" , err )
26+ 			fmt .Printf ("Error accessing path '%s'\n " , inputPath )
2427			return 
2528		}
2629		directory  :=  filepath .Dir (inputPath )
@@ -30,11 +33,25 @@ func processRequest(out string, inputPath string) {
3033		} else  {
3134			outFile  =  filepath .Join (out , fileInfo .Name ()+ ".json" )
3235		}
33- 		writeFileContents (outFile , goastgen .ParseAstFromFile (inputPath ))
36+ 		jsonResult , perr  :=  goastgen .ParseAstFromFile (inputPath )
37+ 		if  perr  !=  nil  {
38+ 			fmt .Printf ("Failed to generate AST for %s\n " , inputPath )
39+ 			return 
40+ 		} else  {
41+ 			err  =  writeFileContents (outFile , jsonResult )
42+ 			if  err  !=  nil  {
43+ 				fmt .Printf ("Error writing AST to output location '%s'\n " , outFile )
44+ 			} else  {
45+ 				fmt .Printf ("Converted AST for %s to %s\n " , inputPath , outFile )
46+ 			}
47+ 			return 
48+ 		}
3449	} else  {
3550		err  :=  filepath .Walk (inputPath , func (path  string , info  os.FileInfo , err  error ) error  {
3651			if  err  !=  nil  {
37- 				fmt .Printf ("Error accessing path %s: %v\n " , path , err )
52+ 				log .SetPrefix ("[ERROR]" )
53+ 				log .Printf ("Error accessing path %s: %v\n " , path , err )
54+ 				fmt .Printf ("Error accessing path '%s'\n " , path )
3855				return  err 
3956			}
4057			if  ! info .IsDir () &&  strings .HasSuffix (info .Name (), ".go" ) {
@@ -45,13 +62,25 @@ func processRequest(out string, inputPath string) {
4562				} else  {
4663					outFile  =  filepath .Join (out , strings .ReplaceAll (directory , inputPath , "" ), info .Name ()+ ".json" )
4764				}
48- 				writeFileContents (outFile , goastgen .ParseAstFromFile (path ))
65+ 				jsonResult , perr  :=  goastgen .ParseAstFromFile (path )
66+ 				if  perr  !=  nil  {
67+ 					fmt .Printf ("Failed to generate AST for %s \n " , path )
68+ 				} else  {
69+ 					err  =  writeFileContents (outFile , jsonResult )
70+ 					if  err  !=  nil  {
71+ 						fmt .Printf ("Error writing AST to output location '%s'\n " , outFile )
72+ 					} else  {
73+ 						fmt .Printf ("Converted AST for %s to %s \n " , path , outFile )
74+ 					}
75+ 					return  nil 
76+ 				}
4977			}
5078			return  nil 
5179		})
5280
5381		if  err  !=  nil  {
54- 			fmt .Printf ("Error walking the path %s: %v\n " , inputPath , err )
82+ 			log .SetPrefix ("[ERROR]" )
83+ 			log .Printf ("Error walking the path %s: %v\n " , inputPath , err )
5584		}
5685	}
5786}
@@ -87,27 +116,31 @@ func parseArguments() (string, string) {
87116	return  out , inputPath 
88117}
89118
90- func  writeFileContents (location  string , contents  string ) {
119+ func  writeFileContents (location  string , contents  string ) error   {
91120	// Open the file for writing (creates a new file if it doesn't exist) 
92121	dir  :=  filepath .Dir (location )
93122
94123	// Create all directories recursively 
95124	err  :=  os .MkdirAll (dir , 0755 )
96125	if  err  !=  nil  {
97- 		fmt .Println ("Failed to create file:" , err )
98- 		return 
126+ 		log .SetPrefix ("[ERROR]" )
127+ 		log .Println ("Failed to create file:" , err )
128+ 		return  err 
99129	}
100130	file , err  :=  os .Create (location )
101131	if  err  !=  nil  {
102- 		fmt .Println ("Failed to create file:" , err )
103- 		return 
132+ 		log .SetPrefix ("[ERROR]" )
133+ 		log .Println ("Failed to create file:" , err )
134+ 		return  err 
104135	}
105136	defer  file .Close ()
106137
107138	// Write the contents to the file 
108139	_ , err  =  file .WriteString (contents )
109140	if  err  !=  nil  {
110- 		fmt .Println ("Failed to write to file:" , err )
111- 		return 
141+ 		log .SetPrefix ("[ERROR]" )
142+ 		log .Println ("Failed to write to file:" , err )
143+ 		return  err 
112144	}
145+ 	return  nil 
113146}
0 commit comments