From 0140bf1319ed119a100eba5db2081faded131255 Mon Sep 17 00:00:00 2001 From: Serge Zaitsev Date: Thu, 20 Sep 2018 12:10:24 +0200 Subject: [PATCH] move meta imports handler into a separate file --- pkg/vcs/meta.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ pkg/vcs/vcs.go | 47 ------------------------------------------- 2 files changed, 53 insertions(+), 47 deletions(-) create mode 100644 pkg/vcs/meta.go diff --git a/pkg/vcs/meta.go b/pkg/vcs/meta.go new file mode 100644 index 0000000..e967b35 --- /dev/null +++ b/pkg/vcs/meta.go @@ -0,0 +1,53 @@ +package vcs + +import ( + "context" + "encoding/xml" + "errors" + "net/http" + "strings" +) + +// MetaImports resolved module import path for certain hosts using the special tag. +func MetaImports(ctx context.Context, module string) (string, error) { + if strings.HasPrefix(module, "github.com/") || strings.HasPrefix(module, "bitbucket.org/") { + return module, nil + } + // TODO: use context + res, err := http.Get("https://" + module + "?go-get=1") + if err != nil { + return "", err + } + defer res.Body.Close() + html := struct { + HTML string `xml:"html"` + Head struct { + Meta []struct { + Name string `xml:"name,attr"` + Content string `xml:"content,attr"` + } `xml:"meta"` + } `xml:"head"` + }{} + dec := xml.NewDecoder(res.Body) + dec.Strict = false + dec.AutoClose = xml.HTMLAutoClose + dec.Entity = xml.HTMLEntity + if err := dec.Decode(&html); err != nil { + return "", err + } + for _, meta := range html.Head.Meta { + if meta.Name == "go-import" { + if f := strings.Fields(meta.Content); len(f) == 3 { + if f[0] != module { + return "", errors.New("prefix does not match the module") + } + url := f[2] + if i := strings.Index(url, "://"); i >= 0 { + url = url[i+3:] + } + return url, nil + } + } + } + return "", errors.New("go-import meta tag not found") +} diff --git a/pkg/vcs/vcs.go b/pkg/vcs/vcs.go index fd04700..0ce8eae 100644 --- a/pkg/vcs/vcs.go +++ b/pkg/vcs/vcs.go @@ -2,10 +2,7 @@ package vcs import ( "context" - "encoding/xml" - "errors" "io" - "net/http" "regexp" "strings" "time" @@ -60,47 +57,3 @@ func Password(username, password string) Auth { return Auth{Username: username, // Key returns an Auth implementation that uses key file authentication mechanism. func Key(key string) Auth { return Auth{Key: key} } - -// MetaImports resolved module import path for certain hosts using the special tag. -func MetaImports(ctx context.Context, module string) (string, error) { - if strings.HasPrefix(module, "github.com/") || strings.HasPrefix(module, "bitbucket.org/") { - return module, nil - } - // TODO: use context - res, err := http.Get("https://" + module + "?go-get=1") - if err != nil { - return "", err - } - defer res.Body.Close() - html := struct { - HTML string `xml:"html"` - Head struct { - Meta []struct { - Name string `xml:"name,attr"` - Content string `xml:"content,attr"` - } `xml:"meta"` - } `xml:"head"` - }{} - dec := xml.NewDecoder(res.Body) - dec.Strict = false - dec.AutoClose = xml.HTMLAutoClose - dec.Entity = xml.HTMLEntity - if err := dec.Decode(&html); err != nil { - return "", err - } - for _, meta := range html.Head.Meta { - if meta.Name == "go-import" { - if f := strings.Fields(meta.Content); len(f) == 3 { - if f[0] != module { - return "", errors.New("prefix does not match the module") - } - url := f[2] - if i := strings.Index(url, "://"); i >= 0 { - url = url[i+3:] - } - return url, nil - } - } - } - return "", errors.New("go-import meta tag not found") -}