diff --git a/bin/update-paths.sh b/bin/update-paths.sh new file mode 100755 index 0000000..201712d --- /dev/null +++ b/bin/update-paths.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +go run ./cmd/replace-paths/replace-paths.go diff --git a/cmd/replace-paths/replace-paths.go b/cmd/replace-paths/replace-paths.go new file mode 100644 index 0000000..e319690 --- /dev/null +++ b/cmd/replace-paths/replace-paths.go @@ -0,0 +1,130 @@ +package main + +import ( + "go/ast" + "go/parser" + "go/token" + "io/fs" + "log" + "os" + "path/filepath" + "strings" +) + +func main() { + rp := ReplacePaths{} + result, err := rp.Run() + if err != nil { + log.Fatal(err) + } + + log.Println(result) +} + +const rootDir string = "./" + +type ReplacePaths struct{} + +func (rp *ReplacePaths) Run() ([]string, error) { + goFiles, err := rp.readGoFiles() + if err != nil { + return nil, err + } + + fileImports, err := rp.readAllFileImports(goFiles) + if err != nil { + return nil, err + } + + importPaths := []string{} + for _, fileImport := range fileImports { + importPaths = append(importPaths, fileImport) + } + + return importPaths, nil +} + +func (rp *ReplacePaths) readGoFiles() ([]string, error) { + files := []string{} + + walk := func(s string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if d.IsDir() { + return nil + } + + if strings.HasSuffix(s, ".go") { + files = append(files, s) + } + + return nil + } + + filepath.WalkDir(rootDir, walk) + + return files, nil +} + +func (rp *ReplacePaths) readAllFileImports(testFiles []string) ([]string, error) { + result := []string{} + + for _, filePath := range testFiles { + funcNames, err := rp.readFileImports(filePath) + if err != nil { + return result, err + } + + result = append(result, funcNames...) + } + + return result, nil +} + +func (rp *ReplacePaths) readFileImports(filePath string) ([]string, error) { + goFile, err := rp.readFile(filePath) + if err != nil { + return nil, err + } + defer goFile.Close() + + declNames := []string{} + fset := token.NewFileSet() + astFile, err := parser.ParseFile(fset, "", goFile, parser.ParseComments) + if err != nil { + return nil, err + } + + for _, decl := range astFile.Decls { + switch t := decl.(type) { + case *ast.GenDecl: + if t.Tok.String() == "import" { + for _, spec := range t.Specs { + switch s := spec.(type) { + case *ast.ImportSpec: + if s.Path != nil { + v := s.Path.Value + log.Println(v) + // TODO(@chris-ramon). + // GitHub Issue: To be completed. + } + } + } + } + + } + } + + return declNames, nil +} + +func (rp *ReplacePaths) readFile(filePath string) (*os.File, error) { + goFile, err := os.Open(filePath) + if err != nil { + return nil, err + } + + return goFile, nil +}