/usr/share/gocode/src/github.com/constabulary/gb/internal/vendor/imports.go is in golang-github-constabulary-gb-dev 0.4.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | package vendor
import (
"fmt"
"go/parser"
"go/token"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// ParseImports parses Go packages from a specific root returning a set of import paths.
func ParseImports(root string) (map[string]bool, error) {
pkgs := make(map[string]bool)
var walkFn = func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
name := info.Name()
if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") || name == "testdata" {
return filepath.SkipDir
}
return nil
}
if filepath.Ext(path) != ".go" { // Parse only go source files
return nil
}
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, path, nil, parser.ImportsOnly)
if err != nil {
return err
}
for _, s := range f.Imports {
p := strings.Replace(s.Path.Value, "\"", "", -1)
pkgs[p] = true
}
return nil
}
err := filepath.Walk(root, walkFn)
return pkgs, err
}
// FetchMetadata fetchs the remote metadata for path.
func FetchMetadata(path string, insecure bool) (rc io.ReadCloser, err error) {
defer func() {
if err != nil {
err = errors.Wrap(err, "unable to determine remote metadata protocol")
}
}()
// try https first
rc, err = fetchMetadata("https", path)
if err == nil {
return
}
// try http if supported
if insecure {
rc, err = fetchMetadata("http", path)
}
return
}
func fetchMetadata(scheme, path string) (io.ReadCloser, error) {
url := fmt.Sprintf("%s://%s?go-get=1", scheme, path)
switch scheme {
case "https", "http":
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to access url %q", url)
}
return resp.Body, nil
default:
return nil, fmt.Errorf("unknown remote protocol scheme: %q", scheme)
}
}
// ParseMetadata fetchs and decodes remote metadata for path.
func ParseMetadata(path string, insecure bool) (string, string, string, error) {
rc, err := FetchMetadata(path, insecure)
if err != nil {
return "", "", "", err
}
defer rc.Close()
imports, err := parseMetaGoImports(rc)
if err != nil {
return "", "", "", err
}
match := -1
for i, im := range imports {
if !strings.HasPrefix(path, im.Prefix) {
continue
}
if match != -1 {
return "", "", "", fmt.Errorf("multiple meta tags match import path %q", path)
}
match = i
}
if match == -1 {
return "", "", "", fmt.Errorf("go-import metadata not found")
}
return imports[match].Prefix, imports[match].VCS, imports[match].RepoRoot, nil
}
|