Skip to content

Commit

Permalink
Support for exclude file (#30)
Browse files Browse the repository at this point in the history
* support for exclude file

* fixed unit test

* fixed usit test
  • Loading branch information
ankit-privado authored Sep 7, 2023
1 parent 43a5df6 commit 893a0d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
28 changes: 17 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"os"
"path/filepath"
"privado.ai/goastgen/goastgen"
"regexp"
"runtime"
"strings"
)

var Version = "dev"

func main() {
out, inputPath := parseArguments()
processRequest(out, inputPath)
out, inputPath, excludeFiles := parseArguments()
processRequest(out, inputPath, excludeFiles)
}

func processFile(out string, inputPath string, path string, info os.FileInfo, resultErr chan error, sem chan int) {
Expand Down Expand Up @@ -50,7 +51,7 @@ func processFile(out string, inputPath string, path string, info os.FileInfo, re
resultErr <- err
}

func processRequest(out string, inputPath string) {
func processRequest(out string, inputPath string, excludeFiles string) {
if strings.HasSuffix(inputPath, ".go") {
fileInfo, err := os.Stat(inputPath)
if err != nil {
Expand Down Expand Up @@ -94,8 +95,11 @@ func processRequest(out string, inputPath string) {
return err
}
if !info.IsDir() && (strings.HasSuffix(info.Name(), ".go") || strings.HasSuffix(info.Name(), ".mod")) {
totalSentForProcessing++
go processFile(out, inputPath, path, info, resultErrChan, sem)
matched, _ := regexp.MatchString(excludeFiles, info.Name())
if excludeFiles == "" || matched == false {
totalSentForProcessing++
go processFile(out, inputPath, path, info, resultErrChan, sem)
}
}
return nil
})
Expand All @@ -118,16 +122,18 @@ func processRequest(out string, inputPath string) {
}
}

func parseArguments() (string, string) {
func parseArguments() (string, string, string) {
var (
out string
inputPath string = ""
version bool
help bool
out string
inputPath string = ""
version bool
help bool
excludeFiles string
)
flag.StringVar(&out, "out", ".ast", "Out put location of ast")
flag.BoolVar(&version, "version", false, "print the version")
flag.BoolVar(&help, "help", false, "print the usage")
flag.StringVar(&excludeFiles, "exclude", "", "regex to exclude files")
flag.Parse()
if version {
fmt.Println(Version)
Expand All @@ -146,7 +152,7 @@ func parseArguments() (string, string) {
flag.PrintDefaults()
os.Exit(1)
}
return out, inputPath
return out, inputPath, excludeFiles
}

func writeFileContents(location string, contents string) error {
Expand Down
8 changes: 4 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func TestProcessRequestWithSingleFileUseCase(t *testing.T) {
"fmt.Println(\"Hello World\")\n" +
"}"
file.WriteString(code)
processRequest(".ast", srcFile)
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)
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")
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestProcessRequestWithMultipleFileDiffFolderStructureUsecase(t *testing.T)
return
}
file.WriteString(code)
processRequest(".ast", newFolder)
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")
Expand All @@ -89,7 +89,7 @@ func TestProcessRequestWithMultipleFileDiffFolderStructureUsecase(t *testing.T)
assert.Nil(t, err, "check the ast output is generated at expected location")

diffOutLocation := filepath.Join(tempDir, uuid.New().String())
processRequest(diffOutLocation, newFolder)
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")
Expand Down

0 comments on commit 893a0d3

Please sign in to comment.