Skip to content

Commit

Permalink
Merge pull request #293 from qor/fix-modfile-path
Browse files Browse the repository at this point in the history
Search the mod file when the current path is not the outermost path
  • Loading branch information
chenxin0723 authored Aug 9, 2024
2 parents 209a49a + cfd5015 commit c66686e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
8 changes: 6 additions & 2 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package admin
import (
"html/template"
"log"
"os"
"path/filepath"
"reflect"

Expand Down Expand Up @@ -112,8 +113,11 @@ func (admin *Admin) RegisterViewPath(pth string) {
var err error
if err = admin.AssetFS.RegisterPath(filepath.Join(utils.AppRoot, "vendor", pth)); err != nil {
for _, gopath := range utils.GOPATH() {
if err = admin.AssetFS.RegisterPath(filepath.Join(gopath, getDepVersionFromMod(pth))); err == nil {
break
startDir, _ := os.Getwd()
if modPath := findGoModFile(startDir); modPath != "" {
if err = admin.AssetFS.RegisterPath(filepath.Join(gopath, getDepVersionFromMod(modPath, pth))); err == nil {
break
}
}

if err = admin.AssetFS.RegisterPath(filepath.Join(gopath, "src", pth)); err == nil {
Expand Down
34 changes: 28 additions & 6 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package admin

import (
"html/template"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -45,8 +45,11 @@ func RegisterViewPath(pth string) {
for _, assetFS := range globalAssetFSes {
if err = assetFS.RegisterPath(filepath.Join(utils.AppRoot, "vendor", pth)); err != nil {
for _, gopath := range utils.GOPATH() {
if err = assetFS.RegisterPath(filepath.Join(gopath, getDepVersionFromMod(pth))); err == nil {
break
startDir, _ := os.Getwd()
if modPath := findGoModFile(startDir); modPath != "" {
if err = assetFS.RegisterPath(filepath.Join(gopath, getDepVersionFromMod(modPath, pth))); err == nil {
break
}
}

if err = assetFS.RegisterPath(filepath.Join(gopath, "src", pth)); err == nil {
Expand All @@ -64,9 +67,9 @@ func equal(a, b interface{}) bool {
return reflect.DeepEqual(a, b)
}

func getDepVersionFromMod(pth string) string {
func getDepVersionFromMod(modPath, pth string) string {
if len(goModDeps) == 0 {
if cont, err := ioutil.ReadFile("go.mod"); err == nil {
if cont, err := os.ReadFile(modPath); err == nil {
goModDeps = strings.Split(string(cont), "\n")
}
}
Expand All @@ -81,5 +84,24 @@ func getDepVersionFromMod(pth string) string {
return pth
}

return getDepVersionFromMod(pth[:strings.LastIndex(pth, "/")]) + pth[strings.LastIndex(pth, "/"):]
return getDepVersionFromMod(modPath, pth[:strings.LastIndex(pth, "/")]) + pth[strings.LastIndex(pth, "/"):]
}

func findGoModFile(startDir string) string {
currentDir := startDir

for {
goModPath := filepath.Join(currentDir, "go.mod")
if _, err := os.Stat(goModPath); err == nil {
return goModPath
}

parentDir := filepath.Dir(currentDir)
if parentDir == currentDir {
break
}
currentDir = parentDir
}

return ""
}
2 changes: 1 addition & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestGetDepVersionFromMod(t *testing.T) {
{view: "no/unknown/nonexistent", want: "no/unknown/nonexistent"},
}
for _, v := range cases {
if got := getDepVersionFromMod(v.view); v.want != got {
if got := getDepVersionFromMod("", v.view); v.want != got {
t.Errorf("GetDepVersionFromMod-viewpath: %v, want: %v, got: %v", v.view, v.want, got)
} else {
fmt.Println(got)
Expand Down

0 comments on commit c66686e

Please sign in to comment.