2020-02-06 15:09:25 +05:00
|
|
|
package golang
|
|
|
|
|
|
|
|
import (
|
|
|
|
// stdlib
|
|
|
|
"bytes"
|
2020-02-07 19:33:16 +05:00
|
|
|
"encoding/xml"
|
2020-02-06 15:09:25 +05:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2020-02-07 21:31:45 +05:00
|
|
|
"sync"
|
2020-02-06 15:09:25 +05:00
|
|
|
|
|
|
|
// local
|
|
|
|
"go.dev.pztrn.name/glp/configuration"
|
|
|
|
"go.dev.pztrn.name/glp/httpclient"
|
|
|
|
"go.dev.pztrn.name/glp/structs"
|
|
|
|
)
|
|
|
|
|
2020-02-07 21:31:45 +05:00
|
|
|
var (
|
|
|
|
goDatas map[string]*godata
|
|
|
|
goDatasMutex sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
// This structure used for caching data about dependencies and prevent
|
|
|
|
// unneeded requests.
|
|
|
|
type godata struct {
|
|
|
|
SourceURLDirTemplate string
|
|
|
|
SourceURLFileTemplate string
|
|
|
|
VCSPath string
|
|
|
|
VCS string
|
|
|
|
}
|
|
|
|
|
2020-02-07 19:33:16 +05:00
|
|
|
// attrValue returns the attribute value for the case-insensitive key
|
|
|
|
// `name', or the empty string if nothing is found.
|
|
|
|
func attrValue(attrs []xml.Attr, name string) string {
|
|
|
|
for _, a := range attrs {
|
|
|
|
if strings.EqualFold(a.Name.Local, name) {
|
|
|
|
return a.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:09:25 +05:00
|
|
|
// Gets go-import and go-source data and fill it in dependency.
|
|
|
|
func getGoData(dependency *structs.Dependency) {
|
2020-02-07 21:31:45 +05:00
|
|
|
// Check if information about that dependency already cached.
|
|
|
|
// Use cached data if so.
|
|
|
|
goDatasMutex.Lock()
|
|
|
|
depInfo, cached := goDatas[dependency.Name+"@"+dependency.Version]
|
|
|
|
goDatasMutex.Unlock()
|
|
|
|
|
|
|
|
if cached {
|
|
|
|
dependency.VCS.SourceURLDirTemplate = depInfo.SourceURLDirTemplate
|
|
|
|
dependency.VCS.SourceURLFileTemplate = depInfo.SourceURLFileTemplate
|
|
|
|
dependency.VCS.VCS = depInfo.VCS
|
|
|
|
dependency.VCS.VCSPath = depInfo.VCSPath
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:09:25 +05:00
|
|
|
// Dependencies are imported using URL which can be called with
|
|
|
|
// "?go-get=1" parameter to obtain required VCS data.
|
|
|
|
req, _ := http.NewRequest("GET", "http://"+dependency.Name, nil)
|
|
|
|
|
|
|
|
q := req.URL.Query()
|
|
|
|
q.Add("go-get", "1")
|
|
|
|
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
respBody := httpclient.GET(req)
|
|
|
|
if respBody == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTML is hard to parse properly statically, so we will go
|
|
|
|
// line-by-line for <head> parsing.
|
|
|
|
resp := bytes.NewBuffer(respBody)
|
|
|
|
|
2020-02-07 19:33:16 +05:00
|
|
|
// Adopted headers parsing algo from Go itself.
|
|
|
|
// See https://github.com/golang/go/blob/95e1ea4598175a3461f40d00ce47a51e5fa6e5ea/src/cmd/go/internal/get/discovery.go
|
|
|
|
|
|
|
|
decoder := xml.NewDecoder(resp)
|
|
|
|
decoder.Strict = false
|
2020-02-06 15:09:25 +05:00
|
|
|
|
|
|
|
for {
|
2020-02-07 19:33:16 +05:00
|
|
|
token, err := decoder.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Fatalln("Failed to parse dependency's go-source and go-import things:", err.Error())
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:09:25 +05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-02-07 19:33:16 +05:00
|
|
|
if e, ok := token.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") {
|
|
|
|
break
|
|
|
|
}
|
2020-02-06 15:09:25 +05:00
|
|
|
|
2020-02-07 19:33:16 +05:00
|
|
|
if e, ok := token.(xml.EndElement); ok && strings.EqualFold(e.Name.Local, "head") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
e, ok := token.(xml.StartElement)
|
|
|
|
if !ok || !strings.EqualFold(e.Name.Local, "meta") {
|
|
|
|
continue
|
2020-02-06 15:09:25 +05:00
|
|
|
}
|
|
|
|
|
2020-02-07 19:33:16 +05:00
|
|
|
// Check if we haven't found "go-import" or "go-source" in token's
|
|
|
|
// attributes.
|
|
|
|
if attrValue(e.Attr, "name") != "go-import" && attrValue(e.Attr, "name") != "go-source" {
|
|
|
|
continue
|
2020-02-06 15:09:25 +05:00
|
|
|
}
|
|
|
|
|
2020-02-07 19:33:16 +05:00
|
|
|
// Parse go-import data first.
|
|
|
|
if attrValue(e.Attr, "name") == "go-import" {
|
|
|
|
if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 {
|
|
|
|
dependency.VCS.VCS = f[1]
|
|
|
|
dependency.VCS.VCSPath = f[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then - go-source data.
|
|
|
|
if attrValue(e.Attr, "name") == "go-source" {
|
|
|
|
if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 4 {
|
|
|
|
dependency.VCS.SourceURLDirTemplate = f[2]
|
|
|
|
dependency.VCS.SourceURLFileTemplate = f[3]
|
|
|
|
}
|
2020-02-06 15:09:25 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.Cfg.Log.Debug {
|
|
|
|
log.Printf("go-import and go-source data parsed: %+v\n", dependency.VCS)
|
|
|
|
}
|
2020-02-07 21:31:45 +05:00
|
|
|
|
|
|
|
// Cache parsed data.
|
|
|
|
goDatasMutex.Lock()
|
|
|
|
goDatas[dependency.Name+"@"+dependency.Version] = &godata{
|
|
|
|
SourceURLDirTemplate: dependency.VCS.SourceURLDirTemplate,
|
|
|
|
SourceURLFileTemplate: dependency.VCS.SourceURLFileTemplate,
|
|
|
|
VCS: dependency.VCS.VCS,
|
|
|
|
VCSPath: dependency.VCS.VCSPath,
|
|
|
|
}
|
|
|
|
goDatasMutex.Unlock()
|
2020-02-06 15:09:25 +05:00
|
|
|
}
|