@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"flag"
5
5
"fmt"
6
+ "log"
6
7
"os"
7
8
"path/filepath"
8
9
"privado.ai/goastgen/goastgen"
@@ -20,7 +21,9 @@ func processRequest(out string, inputPath string) {
20
21
if strings .HasSuffix (inputPath , ".go" ) {
21
22
fileInfo , err := os .Stat (inputPath )
22
23
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 )
24
27
return
25
28
}
26
29
directory := filepath .Dir (inputPath )
@@ -30,11 +33,25 @@ func processRequest(out string, inputPath string) {
30
33
} else {
31
34
outFile = filepath .Join (out , fileInfo .Name ()+ ".json" )
32
35
}
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
+ }
34
49
} else {
35
50
err := filepath .Walk (inputPath , func (path string , info os.FileInfo , err error ) error {
36
51
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 )
38
55
return err
39
56
}
40
57
if ! info .IsDir () && strings .HasSuffix (info .Name (), ".go" ) {
@@ -45,13 +62,25 @@ func processRequest(out string, inputPath string) {
45
62
} else {
46
63
outFile = filepath .Join (out , strings .ReplaceAll (directory , inputPath , "" ), info .Name ()+ ".json" )
47
64
}
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
+ }
49
77
}
50
78
return nil
51
79
})
52
80
53
81
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 )
55
84
}
56
85
}
57
86
}
@@ -87,27 +116,31 @@ func parseArguments() (string, string) {
87
116
return out , inputPath
88
117
}
89
118
90
- func writeFileContents (location string , contents string ) {
119
+ func writeFileContents (location string , contents string ) error {
91
120
// Open the file for writing (creates a new file if it doesn't exist)
92
121
dir := filepath .Dir (location )
93
122
94
123
// Create all directories recursively
95
124
err := os .MkdirAll (dir , 0755 )
96
125
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
99
129
}
100
130
file , err := os .Create (location )
101
131
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
104
135
}
105
136
defer file .Close ()
106
137
107
138
// Write the contents to the file
108
139
_ , err = file .WriteString (contents )
109
140
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
112
144
}
145
+ return nil
113
146
}
0 commit comments