Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
fix: added handling of modules
Browse files Browse the repository at this point in the history
  • Loading branch information
stebenz committed Sep 22, 2020
1 parent 7631fac commit c907ba2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pkg/docu/docu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

type Documentation struct {
tree []*treeelement.TreeElement
tree []*treeelement.TreeElement
modulePath string
}

func New() *Documentation {
Expand Down
51 changes: 51 additions & 0 deletions pkg/modules/modules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package modules

import (
"os"
"os/exec"
"path/filepath"
"strings"
)

type Modules struct {
basePath string
}

func New(basePath string) *Modules {
path := strings.TrimPrefix(basePath, filepath.Join(os.ExpandEnv("$GOPATH"), "src")+"/")
return &Modules{
basePath: getModuleForPath(path),
}
}

func (m *Modules) GetPathForImport(importPath string) string {
if importPath == "" {
return ""
}

if strings.HasPrefix(importPath, m.basePath) {
return filepath.Join(os.ExpandEnv("$GOPATH"), "src", importPath)
}

return filepath.Join(os.ExpandEnv("$GOPATH"), "pkg", "mod", getModuleForPath(importPath))
}

func getModuleForPath(path string) string {
levels := strings.Split(path, "/")

for i := len(levels); i > 0; i-- {
path := filepath.Join(levels[0:i]...)
mod, err := checkGoList(path)
if err == nil {
return strings.Replace(mod, " ", "@", 1)
}
}
return ""
}

func checkGoList(path string) (string, error) {
cmd := exec.Command("sh", "-c", "go list -m "+path)
resultPath, err := cmd.CombinedOutput()
strPath := string(resultPath)
return strings.TrimSuffix(strPath, "\n"), err
}
9 changes: 6 additions & 3 deletions pkg/pack/package.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pack

import (
"github.com/caos/documentation/pkg/modules"
"github.com/caos/documentation/pkg/object"
"github.com/caos/documentation/pkg/treeelement"
"go/ast"
Expand All @@ -14,6 +15,7 @@ import (

type Package struct {
BasePath string
Modules *modules.Modules
Files []string
}

Expand All @@ -22,6 +24,7 @@ func New(path string) *Package {

return &Package{
BasePath: basePath,
Modules: modules.New(basePath),
}
}

Expand Down Expand Up @@ -118,7 +121,7 @@ func (p *Package) recursiveGetElementForStruct(structName string, obj *object.Ob
goFiles := p.GetGoFileList()

for _, path := range goFiles {
treeElement, err := getElementForStructInFile(path, structName, obj)
treeElement, err := getElementForStructInFile(path, structName, obj, p.Modules)
if err != nil {
return nil, err
}
Expand All @@ -130,7 +133,7 @@ func (p *Package) recursiveGetElementForStruct(structName string, obj *object.Ob
return nil, nil
}

func getElementForStructInFile(path string, structName string, obj *object.Object) (*treeelement.TreeElement, error) {
func getElementForStructInFile(path string, structName string, obj *object.Object, modules *modules.Modules) (*treeelement.TreeElement, error) {
src, file, err := parseFile(path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -192,7 +195,7 @@ func getElementForStructInFile(path string, structName string, obj *object.Objec
fieldObj.MapType = m

if i != "" {
importPath := filepath.Join(os.ExpandEnv("$GOPATH"), "src", imports[i])
importPath := modules.GetPathForImport(imports[i])
fieldObj.PackageName = filepath.Base(importPath)
subElement, err := New(importPath).recursiveGetElementForStruct(t, fieldObj)
if err != nil {
Expand Down

0 comments on commit c907ba2

Please sign in to comment.