1
2
3
4
5 package gocommand
6
7 import (
8 "bytes"
9 "context"
10 "fmt"
11 "os"
12 "path/filepath"
13 "regexp"
14 "strings"
15 "time"
16
17 "golang.org/x/mod/semver"
18 )
19
20
21 type ModuleJSON struct {
22 Path string
23 Version string
24 Versions []string
25 Replace *ModuleJSON
26 Time *time.Time
27 Update *ModuleJSON
28 Main bool
29 Indirect bool
30 Dir string
31 GoMod string
32 GoVersion string
33 }
34
35 var modFlagRegexp = regexp.MustCompile(`-mod[ =](\w+)`)
36
37
38
39
40
41 func VendorEnabled(ctx context.Context, inv Invocation, r *Runner) (bool, *ModuleJSON, error) {
42 mainMod, go114, err := getMainModuleAnd114(ctx, inv, r)
43 if err != nil {
44 return false, nil, err
45 }
46
47
48 inv.Verb = "env"
49 inv.Args = []string{"GOFLAGS"}
50 stdout, err := r.Run(ctx, inv)
51 if err != nil {
52 return false, nil, err
53 }
54 goflags := string(bytes.TrimSpace(stdout.Bytes()))
55 matches := modFlagRegexp.FindStringSubmatch(goflags)
56 var modFlag string
57 if len(matches) != 0 {
58 modFlag = matches[1]
59 }
60
61 if modFlag == "vendor" {
62 return true, mainMod, nil
63 } else if modFlag != "" {
64 return false, nil, nil
65 }
66 if mainMod == nil || !go114 {
67 return false, nil, nil
68 }
69
70 if fi, err := os.Stat(filepath.Join(mainMod.Dir, "vendor")); err == nil && fi.IsDir() {
71 if mainMod.GoVersion != "" && semver.Compare("v"+mainMod.GoVersion, "v1.14") >= 0 {
72
73
74 return true, mainMod, nil
75 }
76 }
77 return false, nil, nil
78 }
79
80
81
82
83 func getMainModuleAnd114(ctx context.Context, inv Invocation, r *Runner) (*ModuleJSON, bool, error) {
84 const format = `{{.Path}}
85 {{.Dir}}
86 {{.GoMod}}
87 {{.GoVersion}}
88 {{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}
89 `
90 inv.Verb = "list"
91 inv.Args = []string{"-m", "-f", format}
92 stdout, err := r.Run(ctx, inv)
93 if err != nil {
94 return nil, false, err
95 }
96
97 lines := strings.Split(stdout.String(), "\n")
98 if len(lines) < 5 {
99 return nil, false, fmt.Errorf("unexpected stdout: %q", stdout.String())
100 }
101 mod := &ModuleJSON{
102 Path: lines[0],
103 Dir: lines[1],
104 GoMod: lines[2],
105 GoVersion: lines[3],
106 Main: true,
107 }
108 return mod, lines[4] == "go1.14", nil
109 }
110
111
112
113
114
115 func WorkspaceVendorEnabled(ctx context.Context, inv Invocation, r *Runner) (bool, []*ModuleJSON, error) {
116 inv.Verb = "env"
117 inv.Args = []string{"GOWORK"}
118 stdout, err := r.Run(ctx, inv)
119 if err != nil {
120 return false, nil, err
121 }
122 goWork := string(bytes.TrimSpace(stdout.Bytes()))
123 if fi, err := os.Stat(filepath.Join(filepath.Dir(goWork), "vendor")); err == nil && fi.IsDir() {
124 mainMods, err := getWorkspaceMainModules(ctx, inv, r)
125 if err != nil {
126 return false, nil, err
127 }
128 return true, mainMods, nil
129 }
130 return false, nil, nil
131 }
132
133
134
135 func getWorkspaceMainModules(ctx context.Context, inv Invocation, r *Runner) ([]*ModuleJSON, error) {
136 const format = `{{.Path}}
137 {{.Dir}}
138 {{.GoMod}}
139 {{.GoVersion}}
140 `
141 inv.Verb = "list"
142 inv.Args = []string{"-m", "-f", format}
143 stdout, err := r.Run(ctx, inv)
144 if err != nil {
145 return nil, err
146 }
147
148 lines := strings.Split(strings.TrimSuffix(stdout.String(), "\n"), "\n")
149 if len(lines) < 4 {
150 return nil, fmt.Errorf("unexpected stdout: %q", stdout.String())
151 }
152 mods := make([]*ModuleJSON, 0, len(lines)/4)
153 for i := 0; i < len(lines); i += 4 {
154 mods = append(mods, &ModuleJSON{
155 Path: lines[i],
156 Dir: lines[i+1],
157 GoMod: lines[i+2],
158 GoVersion: lines[i+3],
159 Main: true,
160 })
161 }
162 return mods, nil
163 }
164
View as plain text