github.com/sdboyer/gps@v0.16.3/discovery.go (about)

     1  // Copyright 2012 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gps
     6  
     7  // This code is taken from cmd/go/discovery.go; it is the logic go get itself
     8  // uses to interpret meta imports information.
     9  
    10  import (
    11  	"encoding/xml"
    12  	"fmt"
    13  	"io"
    14  	"strings"
    15  )
    16  
    17  // charsetReader returns a reader for the given charset. Currently
    18  // it only supports UTF-8 and ASCII. Otherwise, it returns a meaningful
    19  // error which is printed by go get, so the user can find why the package
    20  // wasn't downloaded if the encoding is not supported. Note that, in
    21  // order to reduce potential errors, ASCII is treated as UTF-8 (i.e. characters
    22  // greater than 0x7f are not rejected).
    23  func charsetReader(charset string, input io.Reader) (io.Reader, error) {
    24  	switch strings.ToLower(charset) {
    25  	case "ascii":
    26  		return input, nil
    27  	default:
    28  		return nil, fmt.Errorf("can't decode XML document using charset %q", charset)
    29  	}
    30  }
    31  
    32  type metaImport struct {
    33  	Prefix, VCS, RepoRoot string
    34  }
    35  
    36  // parseMetaGoImports returns meta imports from the HTML in r.
    37  // Parsing ends at the end of the <head> section or the beginning of the <body>.
    38  func parseMetaGoImports(r io.Reader) (imports []metaImport, err error) {
    39  	d := xml.NewDecoder(r)
    40  	d.CharsetReader = charsetReader
    41  	d.Strict = false
    42  	var t xml.Token
    43  	for {
    44  		t, err = d.RawToken()
    45  		if err != nil {
    46  			if err == io.EOF || len(imports) > 0 {
    47  				err = nil
    48  			}
    49  			return
    50  		}
    51  		if e, ok := t.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") {
    52  			return
    53  		}
    54  		if e, ok := t.(xml.EndElement); ok && strings.EqualFold(e.Name.Local, "head") {
    55  			return
    56  		}
    57  		e, ok := t.(xml.StartElement)
    58  		if !ok || !strings.EqualFold(e.Name.Local, "meta") {
    59  			continue
    60  		}
    61  		if attrValue(e.Attr, "name") != "go-import" {
    62  			continue
    63  		}
    64  		if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 {
    65  			imports = append(imports, metaImport{
    66  				Prefix:   f[0],
    67  				VCS:      f[1],
    68  				RepoRoot: f[2],
    69  			})
    70  		}
    71  	}
    72  }
    73  
    74  // attrValue returns the attribute value for the case-insensitive key
    75  // `name', or the empty string if nothing is found.
    76  func attrValue(attrs []xml.Attr, name string) string {
    77  	for _, a := range attrs {
    78  		if strings.EqualFold(a.Name.Local, name) {
    79  			return a.Value
    80  		}
    81  	}
    82  	return ""
    83  }