github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/go/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  // +build !cmd_go_bootstrap
     6  
     7  // This code is compiled into the real 'go' binary, but it is not
     8  // compiled into the binary that is built during all.bash, so as
     9  // to avoid needing to build net (and thus use cgo) during the
    10  // bootstrap process.
    11  
    12  package main
    13  
    14  import (
    15  	"encoding/xml"
    16  	"io"
    17  	"strings"
    18  )
    19  
    20  // parseMetaGoImports returns meta imports from the HTML in r.
    21  // Parsing ends at the end of the <head> section or the beginning of the <body>.
    22  func parseMetaGoImports(r io.Reader) (imports []metaImport) {
    23  	d := xml.NewDecoder(r)
    24  	d.Strict = false
    25  	for {
    26  		t, err := d.Token()
    27  		if err != nil {
    28  			return
    29  		}
    30  		if e, ok := t.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") {
    31  			return
    32  		}
    33  		if e, ok := t.(xml.EndElement); ok && strings.EqualFold(e.Name.Local, "head") {
    34  			return
    35  		}
    36  		e, ok := t.(xml.StartElement)
    37  		if !ok || !strings.EqualFold(e.Name.Local, "meta") {
    38  			continue
    39  		}
    40  		if attrValue(e.Attr, "name") != "go-import" {
    41  			continue
    42  		}
    43  		if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 {
    44  			imports = append(imports, metaImport{
    45  				Prefix:   f[0],
    46  				VCS:      f[1],
    47  				RepoRoot: f[2],
    48  			})
    49  		}
    50  	}
    51  }
    52  
    53  // attrValue returns the attribute value for the case-insensitive key
    54  // `name', or the empty string if nothing is found.
    55  func attrValue(attrs []xml.Attr, name string) string {
    56  	for _, a := range attrs {
    57  		if strings.EqualFold(a.Name.Local, name) {
    58  			return a.Value
    59  		}
    60  	}
    61  	return ""
    62  }