Skip to content

Commit ac326d1

Browse files
logging metadata information of processing (#21)
1. Added stdout message to be processed by invoking utility. 2. Better error handling mechanism and subsequently logging the error situation for given file on stout. Note: where messages are printed with `log` it will print it to `stderr`. `fmt.println` and its other version will print the message to `stdout`
1 parent 731a9c1 commit ac326d1

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

Diff for: goastgen/libgoastgen.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ import (
2121
Returns:
2222
If given source is valid go source then it will generate AST in JSON format other will return "" string.
2323
*/
24-
func ParseAstFromSource(filename string, src any) string {
24+
func ParseAstFromSource(filename string, src any) (string, error) {
2525
fset := token.NewFileSet()
2626
parsedAst, err := parser.ParseFile(fset, filename, src, 0)
2727
if err != nil {
2828
// TODO: convert this to just warning error log.
29-
log.Fatal(err)
29+
log.SetPrefix("[ERROR]")
30+
log.Println("Error while parsing source from source file -> '", filename, "'")
31+
log.Print(err)
32+
return "", err
3033
}
3134
result := serilizeToMap(parsedAst, fset)
3235
return serilizeToJsonStr(result)
@@ -42,14 +45,15 @@ func ParseAstFromSource(filename string, src any) string {
4245
Returns:
4346
If given directory contains valid go source code then it will generate AST in JSON format otherwise will return "" string.
4447
*/
45-
func ParseAstFromDir(dir string) string {
48+
func ParseAstFromDir(dir string) (string, error) {
4649
fset := token.NewFileSet()
4750
parsedAst, err := parser.ParseDir(fset, dir, nil, 0)
4851
if err != nil {
4952
// TODO: convert this to just warning error log.
5053
log.SetPrefix("[ERROR]")
51-
log.Println("Error while parsing source from source directory -> '", dir, ",")
54+
log.Println("Error while parsing source from source directory -> '", dir, "'")
5255
log.Print(err)
56+
return "", err
5357
}
5458
result := serilizeToMap(parsedAst, fset)
5559
return serilizeToJsonStr(result)
@@ -65,19 +69,18 @@ func ParseAstFromDir(dir string) string {
6569
Returns:
6670
If given file is a valid go code then it will generate AST in JSON format otherwise will return "" string.
6771
*/
68-
func ParseAstFromFile(file string) string {
72+
func ParseAstFromFile(file string) (string, error) {
6973
fset := token.NewFileSet()
7074
// NOTE: Haven't explore much of mode parameter. Default value has been passed as 0
7175
parsedAst, err := parser.ParseFile(fset, file, nil, 0)
7276
if err != nil {
7377
log.SetPrefix("[ERROR]")
7478
log.Println("Error while parsing source file -> '", file, ",")
7579
log.Print(err)
76-
return ""
77-
} else {
78-
result := serilizeToMap(parsedAst, fset)
79-
return serilizeToJsonStr(result)
80+
return "", err
8081
}
82+
result := serilizeToMap(parsedAst, fset)
83+
return serilizeToJsonStr(result)
8184
}
8285

8386
/*
@@ -89,14 +92,15 @@ func ParseAstFromFile(file string) string {
8992
Returns:
9093
JSON string
9194
*/
92-
func serilizeToJsonStr(objectMap interface{}) string {
95+
func serilizeToJsonStr(objectMap interface{}) (string, error) {
9396
jsonStr, err := json.MarshalIndent(objectMap, "", " ")
9497
if err != nil {
9598
log.SetPrefix("[ERROR]")
9699
log.Println("Error while generating the AST JSON")
97-
log.Print(err)
100+
log.Println(err)
101+
return "", err
98102
}
99-
return string(jsonStr)
103+
return string(jsonStr), nil
100104
}
101105

102106
/*

Diff for: goastgen/libgoastgen_ast_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestFirst(t *testing.T) {
3838
"fmt.Println(\"Hello World\")\n" +
3939
"}"
4040

41-
result := ParseAstFromSource("helloworld.go", code)
41+
result, _ := ParseAstFromSource("helloworld.go", code)
4242
fmt.Println(result)
4343

4444
}

Diff for: main.go

+45-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
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

Comments
 (0)