diff --git a/here/dir.go b/here/dir.go index 8d5fe47..033e696 100644 --- a/here/dir.go +++ b/here/dir.go @@ -2,6 +2,7 @@ package here import ( "encoding/json" + "fmt" "os" "path" "path/filepath" @@ -39,20 +40,30 @@ func Dir(p string) (Info, error) { return i, err } + if strings.Contains(es, "can't load package: package .") { + if _, err := os.Stat(fmt.Sprintf("%s/go.mod", p)); err == nil { + var mod Module + bm, err := run ("go", "list", "-m", "-json") + if err != nil { + return i, err + } + + if err := json.Unmarshal(bm, &mod); err != nil { + return i, err + } + info := NewInfoFromPath(p, mod) + prepareInfo(p, info, &i) + + return i, err + } + } + info, err := Dir(filepath.Dir(p)) if err != nil { return info, err } - i.Module = info.Module - - ph := strings.TrimPrefix(p, info.Module.Dir) - - i.ImportPath = path.Join(info.Module.Path, ph) - i.Name = path.Base(i.ImportPath) - - ph = filepath.Join(info.Module.Dir, ph) - i.Dir = ph + prepareInfo(p, info, &i) return i, err } @@ -73,3 +84,14 @@ func Dir(p string) (Info, error) { return i, nil } + +func prepareInfo(p string, info Info, target *Info) { + target.Module = info.Module + ph := strings.TrimPrefix(p, target.Module.Dir) + + target.ImportPath = path.Join(info.Module.Path, ph) + target.Name = path.Base(target.ImportPath) + + ph = filepath.Join(info.Module.Dir, ph) + target.Dir = ph +} \ No newline at end of file diff --git a/here/info.go b/here/info.go index 03efc11..2ac9489 100644 --- a/here/info.go +++ b/here/info.go @@ -16,6 +16,16 @@ type Info struct { Module Module } +// NewInfoFromPath initialize a Info with a basic information and his module +// this method could be used when the Unmarshal information is not possible +func NewInfoFromPath(path string, m Module) Info { + return Info{ + Dir: path, + ImportPath: "command-line-arguments", + Module: m, + } +} + func (fi Info) MarshalJSON() ([]byte, error) { mm := map[string]interface{}{ "ImportPath": fi.ImportPath,