pkger/here/pkg.go

48 lines
1009 B
Go
Raw Permalink Normal View History

2019-08-05 00:13:27 +03:00
package here
2019-09-12 04:29:39 +03:00
import (
"encoding/json"
2019-11-01 22:53:54 +03:00
"path"
"strings"
2019-09-12 04:29:39 +03:00
)
2019-08-05 00:13:27 +03:00
// Package attempts to gather info for the requested package.
//
// From the `go help list` docs:
// The -find flag causes list to identify the named packages but not
// resolve their dependencies: the Imports and Deps lists will be empty.
//
// A workaround for this issue is to use the `Dir` field in the
// returned `Info` value and pass it to the `Dir(string) (Info, error)`
// function to return the complete data.
func Package(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
b, err := run("go", "list", "-json", "-find", p)
if err != nil {
2019-11-01 22:53:54 +03:00
if !strings.Contains(err.Error(), "can't load package: package") {
return i, err
}
p, _ = path.Split(p)
return Package(p)
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.Dir, func(p string) (Info, error) {
return i, nil
})
return i, nil
2019-08-05 00:13:27 +03:00
}