-
Notifications
You must be signed in to change notification settings - Fork 687
/
modules_txt.go
67 lines (60 loc) · 1.45 KB
/
modules_txt.go
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
package main
import (
"bufio"
"os"
"os/exec"
"path"
"strings"
"github.com/pkg/errors"
"github.com/datawire/ambassador/pkg/mkopensource/golist"
)
func VendorList() ([]golist.Package, error) {
cmd := exec.Command("go", "mod", "vendor")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, errors.Wrapf(err, "%q", []string{"go", "mod", "vendor"})
}
file, err := os.Open("vendor/modules.txt")
if err != err {
return nil, err
}
defer file.Close()
var pkgs []golist.Package
var curModule *golist.Module
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
parts := strings.Split(line, " ")
switch len(parts) {
case 3:
// ok
case 5, 6:
if parts[3] != "=>" {
return nil, errors.Errorf("malformed line in vendor/modules.txt: %q", line)
}
default:
return nil, errors.Errorf("malformed line in vendor/modules.txt: %q", line)
}
modname := parts[1]
modules, err := golist.GoListModules([]string{"-mod=vendor"}, []string{modname})
if err != nil {
return nil, err
}
if len(modules) != 1 {
return nil, errors.New("unexpected output from go list")
}
curModule = &modules[0]
} else {
pkgname := line
pkgs = append(pkgs, golist.Package{
Dir: "vendor/" + pkgname,
ImportPath: pkgname,
Name: path.Base(pkgname),
Module: curModule,
DepOnly: true,
})
}
}
return pkgs, nil
}