-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. CLI impelmentation which will take the input location as well as optional output location (if not passed it will create .ast folder within the input location). 2. Little code refactor 3. Changed code to add file information only inside root node of type ast.File 4. Unit tests for different use cases for CLI inputs.
- Loading branch information
1 parent
fe66677
commit def385a
Showing
6 changed files
with
202 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,103 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"privado.ai/goastgen/goastgen" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
args := os.Args[1:] | ||
path := args[0] | ||
resultJson := goastgen.ParseAstFromFile(path) | ||
fmt.Println(resultJson) | ||
out, inputPath := parseArguments() | ||
processRequest(out, inputPath) | ||
} | ||
|
||
// go build -buildmode=c-shared -o lib-goastgen.dylib main.go | ||
func processRequest(out string, inputPath string) { | ||
if strings.HasSuffix(inputPath, ".go") { | ||
fileInfo, err := os.Stat(inputPath) | ||
if err != nil { | ||
fmt.Println("Failed to get file info:", err) | ||
return | ||
} | ||
directory := filepath.Dir(inputPath) | ||
var outFile = "" | ||
if out == ".ast" { | ||
outFile = filepath.Join(directory, out, fileInfo.Name()+".json") | ||
} else { | ||
outFile = filepath.Join(out, fileInfo.Name()+".json") | ||
} | ||
writeFileContents(outFile, goastgen.ParseAstFromFile(inputPath)) | ||
} else { | ||
err := filepath.Walk(inputPath, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
fmt.Printf("Error accessing path %s: %v\n", path, err) | ||
return err | ||
} | ||
if !info.IsDir() && strings.HasSuffix(info.Name(), ".go") { | ||
var outFile = "" | ||
directory := filepath.Dir(path) | ||
if out == ".ast" { | ||
outFile = filepath.Join(inputPath, out, strings.ReplaceAll(directory, inputPath, ""), info.Name()+".json") | ||
} else { | ||
outFile = filepath.Join(out, strings.ReplaceAll(directory, inputPath, ""), info.Name()+".json") | ||
} | ||
writeFileContents(outFile, goastgen.ParseAstFromFile(path)) | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
fmt.Printf("Error walking the path %s: %v\n", inputPath, err) | ||
} | ||
} | ||
} | ||
|
||
func parseArguments() (string, string) { | ||
var ( | ||
out string | ||
inputPath string = "" | ||
) | ||
flag.StringVar(&out, "out", ".ast", "Out put location of ast") | ||
flag.Parse() | ||
// Check if positional arguments exist | ||
if flag.NArg() > 0 { | ||
// Retrieve positional arguments | ||
inputPath = flag.Arg(0) | ||
} | ||
if inputPath == "" { | ||
fmt.Println("Usage:") | ||
fmt.Println("\tgoastgen [falgs] <source location>") | ||
fmt.Println() | ||
fmt.Println("Flags:") | ||
flag.PrintDefaults() | ||
os.Exit(1) | ||
} | ||
return out, inputPath | ||
} | ||
|
||
func writeFileContents(location string, contents string) { | ||
// Open the file for writing (creates a new file if it doesn't exist) | ||
dir := filepath.Dir(location) | ||
|
||
// Create all directories recursively | ||
err := os.MkdirAll(dir, 0755) | ||
if err != nil { | ||
fmt.Println("Failed to create file:", err) | ||
return | ||
} | ||
file, err := os.Create(location) | ||
if err != nil { | ||
fmt.Println("Failed to create file:", err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
// Write the contents to the file | ||
_, err = file.WriteString(contents) | ||
if err != nil { | ||
fmt.Println("Failed to write to file:", err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestProcessRequestWithSingleFileUseCase(t *testing.T) { | ||
// Get the temporary directory path | ||
tempDir := os.TempDir() | ||
|
||
// Create a new folder in the temporary directory | ||
newFolder := filepath.Join(tempDir, uuid.New().String()) | ||
err := os.Mkdir(newFolder, 0755) | ||
if err != nil { | ||
fmt.Println("Failed to create folder:", err) | ||
return | ||
} | ||
srcFile := filepath.Join(newFolder, "hello.go") | ||
file, errf := os.Create(srcFile) | ||
if errf != nil { | ||
fmt.Println("Failed to create file:", errf) | ||
return | ||
} | ||
code := "package main \n" + | ||
"import \"fmt\"\n" + | ||
"func main() {\n" + | ||
"fmt.Println(\"Hello World\")\n" + | ||
"}" | ||
file.WriteString(code) | ||
processRequest(".ast", srcFile) | ||
expectedJsonFileLocation := filepath.Join(newFolder, ".ast", "hello.go.json") | ||
_, err = os.Stat(expectedJsonFileLocation) | ||
assert.Nil(t, err, "check the ast output is generated at expected location") | ||
|
||
diffOutLocation := filepath.Join(tempDir, uuid.New().String()) | ||
processRequest(diffOutLocation, srcFile) | ||
expectedJsonFileLocation = filepath.Join(diffOutLocation, "hello.go.json") | ||
_, err = os.Stat(expectedJsonFileLocation) | ||
assert.Nil(t, err, "check the ast output is generated at expected location") | ||
} | ||
|
||
func TestProcessRequestWithMultipleFileDiffFolderStructureUsecase(t *testing.T) { | ||
// Get the temporary directory path | ||
tempDir := os.TempDir() | ||
|
||
// Create a new folder in the temporary directory | ||
newFolder := filepath.Join(tempDir, uuid.New().String()) | ||
err := os.Mkdir(newFolder, 0755) | ||
if err != nil { | ||
fmt.Println("Failed to create folder:", err) | ||
return | ||
} | ||
srcFile := filepath.Join(newFolder, "hello.go") | ||
file, errf := os.Create(srcFile) | ||
if errf != nil { | ||
fmt.Println("Failed to create file:", errf) | ||
return | ||
} | ||
code := "package main \n" + | ||
"import \"fmt\"\n" + | ||
"func main() {\n" + | ||
"fmt.Println(\"Hello World\")\n" + | ||
"}" | ||
file.WriteString(code) | ||
subDir := filepath.Join(newFolder, "subdir") | ||
err = os.Mkdir(subDir, 0755) | ||
if err != nil { | ||
fmt.Println("Failed to create folder:", err) | ||
return | ||
} | ||
subSrcFile := filepath.Join(subDir, "hellosub.go") | ||
file, errf = os.Create(subSrcFile) | ||
if errf != nil { | ||
fmt.Println("Failed to create file:", errf) | ||
return | ||
} | ||
file.WriteString(code) | ||
processRequest(".ast", newFolder) | ||
expectedJsonFileLocationone := filepath.Join(newFolder, ".ast", "hello.go.json") | ||
_, err = os.Stat(expectedJsonFileLocationone) | ||
assert.Nil(t, err, "check the ast output is generated at expected location") | ||
expectedJsonFileLocationtwo := filepath.Join(newFolder, ".ast", "subdir", "hellosub.go.json") | ||
_, err = os.Stat(expectedJsonFileLocationtwo) | ||
assert.Nil(t, err, "check the ast output is generated at expected location") | ||
|
||
diffOutLocation := filepath.Join(tempDir, uuid.New().String()) | ||
processRequest(diffOutLocation, newFolder) | ||
expectedJsonFileLocationone = filepath.Join(diffOutLocation, "hello.go.json") | ||
_, err = os.Stat(expectedJsonFileLocationone) | ||
assert.Nil(t, err, "check the ast output is generated at expected location") | ||
expectedJsonFileLocationtwo = filepath.Join(diffOutLocation, "subdir", "hellosub.go.json") | ||
_, err = os.Stat(expectedJsonFileLocationtwo) | ||
assert.Nil(t, err, "check the ast output is generated at expected location") | ||
} |