Skip to content

Commit f7bfca4

Browse files
authored
Merge pull request #5 from chris-ramon/cmd-replace-paths
{cmd,bin}: adds ./bin/update-paths.sh
2 parents e4d2078 + 06c7e85 commit f7bfca4

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

bin/update-paths.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
go run ./cmd/replace-paths/replace-paths.go

cmd/replace-paths/replace-paths.go

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package main
2+
3+
import (
4+
"go/ast"
5+
"go/parser"
6+
"go/token"
7+
"io/fs"
8+
"log"
9+
"os"
10+
"path/filepath"
11+
"strings"
12+
)
13+
14+
func main() {
15+
rp := ReplacePaths{}
16+
result, err := rp.Run()
17+
if err != nil {
18+
log.Fatal(err)
19+
}
20+
21+
log.Println(result)
22+
}
23+
24+
const rootDir string = "./"
25+
26+
type ReplacePaths struct{}
27+
28+
func (rp *ReplacePaths) Run() ([]string, error) {
29+
goFiles, err := rp.readGoFiles()
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
fileImports, err := rp.readAllFileImports(goFiles)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
importPaths := []string{}
40+
for _, fileImport := range fileImports {
41+
importPaths = append(importPaths, fileImport)
42+
}
43+
44+
return importPaths, nil
45+
}
46+
47+
func (rp *ReplacePaths) readGoFiles() ([]string, error) {
48+
files := []string{}
49+
50+
walk := func(s string, d fs.DirEntry, err error) error {
51+
if err != nil {
52+
return err
53+
}
54+
55+
if d.IsDir() {
56+
return nil
57+
}
58+
59+
if strings.HasSuffix(s, ".go") {
60+
files = append(files, s)
61+
}
62+
63+
return nil
64+
}
65+
66+
filepath.WalkDir(rootDir, walk)
67+
68+
return files, nil
69+
}
70+
71+
func (rp *ReplacePaths) readAllFileImports(testFiles []string) ([]string, error) {
72+
result := []string{}
73+
74+
for _, filePath := range testFiles {
75+
funcNames, err := rp.readFileImports(filePath)
76+
if err != nil {
77+
return result, err
78+
}
79+
80+
result = append(result, funcNames...)
81+
}
82+
83+
return result, nil
84+
}
85+
86+
func (rp *ReplacePaths) readFileImports(filePath string) ([]string, error) {
87+
goFile, err := rp.readFile(filePath)
88+
if err != nil {
89+
return nil, err
90+
}
91+
defer goFile.Close()
92+
93+
declNames := []string{}
94+
fset := token.NewFileSet()
95+
astFile, err := parser.ParseFile(fset, "", goFile, parser.ParseComments)
96+
if err != nil {
97+
return nil, err
98+
}
99+
100+
for _, decl := range astFile.Decls {
101+
switch t := decl.(type) {
102+
case *ast.GenDecl:
103+
if t.Tok.String() == "import" {
104+
for _, spec := range t.Specs {
105+
switch s := spec.(type) {
106+
case *ast.ImportSpec:
107+
if s.Path != nil {
108+
v := s.Path.Value
109+
log.Println(v)
110+
// TODO(@chris-ramon).
111+
// GitHub Issue: To be completed.
112+
}
113+
}
114+
}
115+
}
116+
117+
}
118+
}
119+
120+
return declNames, nil
121+
}
122+
123+
func (rp *ReplacePaths) readFile(filePath string) (*os.File, error) {
124+
goFile, err := os.Open(filePath)
125+
if err != nil {
126+
return nil, err
127+
}
128+
129+
return goFile, nil
130+
}

0 commit comments

Comments
 (0)