Skip to content

{cmd,bin}: adds ./bin/update-paths.sh #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/update-paths.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

go run ./cmd/replace-paths/replace-paths.go
130 changes: 130 additions & 0 deletions cmd/replace-paths/replace-paths.go
Original file line number Diff line number Diff line change
@@ -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
}