2019-08-05 00:13:27 +03:00
|
|
|
package here
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-11-09 22:43:22 +03:00
|
|
|
"fmt"
|
2019-08-05 00:13:27 +03:00
|
|
|
"os"
|
2019-10-26 00:11:34 +03:00
|
|
|
"path"
|
2019-08-05 00:13:27 +03:00
|
|
|
"path/filepath"
|
2019-10-26 00:11:34 +03:00
|
|
|
"strings"
|
2019-08-05 00:13:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Dir attempts to gather info for the requested directory.
|
|
|
|
func Dir(p string) (Info, error) {
|
2019-09-21 19:51:29 +03:00
|
|
|
i, err := Cache(p, func(p string) (Info, error) {
|
2019-09-12 04:29:39 +03:00
|
|
|
var i Info
|
2019-08-05 00:13:27 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
fi, err := os.Stat(p)
|
|
|
|
if err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
2019-08-05 00:13:27 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
if !fi.IsDir() {
|
|
|
|
p = filepath.Dir(p)
|
|
|
|
}
|
2019-08-05 00:13:27 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
2019-08-05 00:13:27 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
defer os.Chdir(pwd)
|
2019-08-05 00:13:27 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
os.Chdir(p)
|
2019-08-05 00:13:27 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
b, err := run("go", "list", "-json")
|
|
|
|
if err != nil {
|
2019-10-26 00:11:34 +03:00
|
|
|
|
2019-11-01 22:53:54 +03:00
|
|
|
es := err.Error()
|
2019-11-11 01:22:01 +03:00
|
|
|
if strings.Contains(es, "cannot find module for path .") || strings.Contains(es, "no Go files") || strings.Contains(es, "can't load package") {
|
2019-11-09 22:43:22 +03:00
|
|
|
if _, err := os.Stat(fmt.Sprintf("%s/go.mod", p)); err == nil {
|
|
|
|
var mod Module
|
2019-11-11 01:22:01 +03:00
|
|
|
bm, err := run("go", "list", "-m", "-json")
|
2019-11-09 22:43:22 +03:00
|
|
|
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
|
|
|
|
}
|
2019-11-10 16:11:07 +03:00
|
|
|
} else {
|
|
|
|
return i, err
|
2019-11-09 22:43:22 +03:00
|
|
|
}
|
|
|
|
|
2019-10-26 00:11:34 +03:00
|
|
|
info, err := Dir(filepath.Dir(p))
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
2019-11-09 22:43:22 +03:00
|
|
|
prepareInfo(p, info, &i)
|
2019-09-12 04:29:39 +03:00
|
|
|
return i, err
|
|
|
|
}
|
2019-08-05 00:13:27 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
if err := json.Unmarshal(b, &i); err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
2019-08-05 00:13:27 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
return i, nil
|
|
|
|
})
|
2019-09-21 19:51:29 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache(i.ImportPath, func(p string) (Info, error) {
|
|
|
|
return i, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return i, nil
|
2019-08-05 00:13:27 +03:00
|
|
|
}
|
2019-11-09 22:43:22 +03:00
|
|
|
|
|
|
|
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
|
2019-11-11 01:22:01 +03:00
|
|
|
}
|