Skip to content

Commit ded27bc

Browse files
committed
Go: Replace exec.Command("go" with toolchain.GoCommand(
1 parent 866fc6b commit ded27bc

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ func restoreRepoLayout(fromDir string, dirEntries []string, scratchDirName strin
6767

6868
// addVersionToMod add a go version directive, e.g. `go 1.14` to a `go.mod` file.
6969
func addVersionToMod(version string) bool {
70-
cmd := exec.Command("go", "mod", "edit", "-go="+version)
70+
cmd := toolchain.GoCommand("mod", "edit", "-go="+version)
7171
return util.RunCmd(cmd)
7272
}
7373

7474
// checkVendor tests to see whether a vendor directory is inconsistent according to the go frontend
7575
func checkVendor() bool {
76-
vendorCheckCmd := exec.Command("go", "list", "-mod=vendor", "./...")
76+
vendorCheckCmd := toolchain.GoCommand("list", "-mod=vendor", "./...")
7777
outp, err := vendorCheckCmd.CombinedOutput()
7878
if err != nil {
7979
badVendorRe := regexp.MustCompile(`(?m)^go: inconsistent vendoring in .*:$`)
@@ -438,7 +438,7 @@ func installDependencies(workspace project.GoWorkspace) {
438438
util.RunCmd(vendor)
439439
}
440440

441-
install = exec.Command("go", "get", "-v", "./...")
441+
install = toolchain.GoCommand("get", "-v", "./...")
442442
install.Dir = path
443443
log.Printf("Installing dependencies using `go get -v ./...` in `%s`.\n", path)
444444
util.RunCmd(install)

go/extractor/toolchain/toolchain.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,14 @@ func SupportsWorkspaces() bool {
137137
return GetEnvGoSemVer().IsAtLeast(V1_18)
138138
}
139139

140+
// Constructs a `*exec.Cmd` for `go` with the specified arguments.
141+
func GoCommand(arg ...string) *exec.Cmd {
142+
return exec.Command("go", arg...)
143+
}
144+
140145
// Run `go mod tidy -e` in the directory given by `path`.
141146
func TidyModule(path string) *exec.Cmd {
142-
cmd := exec.Command("go", "mod", "tidy", "-e")
147+
cmd := GoCommand("mod", "tidy", "-e")
143148
cmd.Dir = path
144149
return cmd
145150
}
@@ -159,21 +164,21 @@ func InitModule(path string) *exec.Cmd {
159164
}
160165
}
161166

162-
modInit := exec.Command("go", "mod", "init", moduleName)
167+
modInit := GoCommand("mod", "init", moduleName)
163168
modInit.Dir = path
164169
return modInit
165170
}
166171

167172
// Constructs a command to run `go mod vendor -e` in the directory given by `path`.
168173
func VendorModule(path string) *exec.Cmd {
169-
modVendor := exec.Command("go", "mod", "vendor", "-e")
174+
modVendor := GoCommand("mod", "vendor", "-e")
170175
modVendor.Dir = path
171176
return modVendor
172177
}
173178

174179
// Constructs a command to run `go version`.
175180
func Version() *exec.Cmd {
176-
version := exec.Command("go", "version")
181+
version := GoCommand("version")
177182
return version
178183
}
179184

@@ -209,7 +214,7 @@ func RunListWithEnv(format string, patterns []string, additionalEnv []string, fl
209214
func ListWithEnv(format string, patterns []string, additionalEnv []string, flags ...string) *exec.Cmd {
210215
args := append([]string{"list", "-e", "-f", format}, flags...)
211216
args = append(args, patterns...)
212-
cmd := exec.Command("go", args...)
217+
cmd := GoCommand(args...)
213218
cmd.Env = append(os.Environ(), additionalEnv...)
214219
return cmd
215220
}

0 commit comments

Comments
 (0)