Skip to content

Commit a48745a

Browse files
committed
Use ProxiedCmd for go commands
1 parent 59002a5 commit a48745a

4 files changed

Lines changed: 30 additions & 31 deletions

File tree

‎go/extractor/autobuilder/autobuilder.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package autobuilder
55
import (
66
"log"
77
"os"
8-
"os/exec"
98

9+
"github.com/github/codeql-go/extractor/registries"
1010
"github.com/github/codeql-go/extractor/util"
1111
)
1212

@@ -68,7 +68,7 @@ func tryBuildIfExists(buildFile, cmd string, args ...string) (scriptSuccess bool
6868
// tryBuild tries to run `cmd args...`, returning true if successful and false if not.
6969
func tryBuild(cmd string, args ...string) bool {
7070
log.Printf("Trying build command %s %v", cmd, args)
71-
res := util.RunCmd(exec.Command(cmd, args...))
71+
res := registries.Command(cmd, args...).RunCmd()
7272
return res && (!CheckExtracted || checkExtractorRun())
7373
}
7474

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func restoreRepoLayout(fromDir string, dirEntries []string, scratchDirName strin
6969
// addVersionToMod add a go version directive, e.g. `go 1.14` to a `go.mod` file.
7070
func addVersionToMod(version string) bool {
7171
cmd := toolchain.GoCommand("mod", "edit", "-go="+version)
72-
return util.RunCmd(cmd)
72+
return cmd.RunCmd()
7373
}
7474

7575
// checkVendor tests to see whether a vendor directory is inconsistent according to the go frontend
@@ -170,7 +170,7 @@ func tryUpdateGoModAndGoSum(workspace project.GoWorkspace) {
170170

171171
// run `go mod tidy -e`
172172
cmd := goMod.Tidy()
173-
res := util.RunCmd(cmd)
173+
res := cmd.RunCmd()
174174

175175
if !res {
176176
log.Printf("Failed to run `go mod tidy -e` in %s\n", goModDir)
@@ -436,13 +436,13 @@ func installDependencies(workspace project.GoWorkspace) {
436436
if util.DirExists(filepath.Join(path, "vendor")) {
437437
vendor := module.Vendor()
438438
log.Printf("Synchronizing vendor file using `go mod vendor` in %s.\n", path)
439-
util.RunCmd(vendor)
439+
vendor.RunCmd()
440440
}
441441

442-
install = toolchain.GoCommand("get", "-v", "./...")
443-
install.Dir = path
442+
goGet := toolchain.GoCommand("get", "-v", "./...")
443+
goGet.SetDir(path)
444444
log.Printf("Installing dependencies using `go get -v ./...` in `%s`.\n", path)
445-
util.RunCmd(install)
445+
goGet.RunCmd()
446446
}
447447
}
448448
}

‎go/extractor/project/project.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package project
33
import (
44
"log"
55
"os"
6-
"os/exec"
76
"path/filepath"
87
"regexp"
98
"slices"
109
"sort"
1110
"strings"
1211

1312
"github.com/github/codeql-go/extractor/diagnostics"
13+
"github.com/github/codeql-go/extractor/registries"
1414
"github.com/github/codeql-go/extractor/toolchain"
1515
"github.com/github/codeql-go/extractor/util"
1616
"golang.org/x/mod/modfile"
@@ -49,12 +49,12 @@ func (module *GoModule) RequiredGoVersion() util.SemVer {
4949
}
5050

5151
// Runs `go mod tidy` for this module.
52-
func (module *GoModule) Tidy() *exec.Cmd {
52+
func (module *GoModule) Tidy() *registries.ProxiedCmd {
5353
return toolchain.TidyModule(filepath.Dir(module.Path))
5454
}
5555

5656
// Runs `go mod vendor -e` for this module.
57-
func (module *GoModule) Vendor() *exec.Cmd {
57+
func (module *GoModule) Vendor() *registries.ProxiedCmd {
5858
return toolchain.VendorModule(filepath.Dir(module.Path))
5959
}
6060

@@ -149,7 +149,7 @@ func InitGoModForLegacyProject(path string) {
149149

150150
modInit := toolchain.InitModule(path)
151151

152-
if !util.RunCmd(modInit) {
152+
if !modInit.RunCmd() {
153153
log.Printf("Failed to initialize go.mod file for this project.")
154154
return
155155
}

‎go/extractor/toolchain/toolchain.go‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func GetEnvGoVersion() string {
4848
// Go version in tests and also allows users to override the system default more generally.
4949
_, hasToolchainVar := os.LookupEnv("GOTOOLCHAIN")
5050
if !hasToolchainVar {
51-
cmd.Env = append(os.Environ(), "GOTOOLCHAIN=local")
51+
cmd.AppendEnv("GOTOOLCHAIN=local")
5252
}
5353

5454
out, err := cmd.CombinedOutput()
@@ -82,10 +82,10 @@ func InstallVersion(workingDir string, version util.SemVer) bool {
8282
// should then work even with an invalid version that's still in `go.mod`
8383
toolchainArg := "GOTOOLCHAIN=go" + version.String()[1:]
8484
versionCmd := Version()
85-
versionCmd.Dir = workingDir
86-
versionCmd.Env = append(os.Environ(), toolchainArg)
87-
versionCmd.Stdout = os.Stdout
88-
versionCmd.Stderr = os.Stderr
85+
versionCmd.SetDir(workingDir)
86+
versionCmd.AppendEnv(toolchainArg)
87+
versionCmd.SetStdout(os.Stdout)
88+
versionCmd.SetStderr(os.Stderr)
8989

9090
log.Printf(
9191
"Trying to install Go %s using its canonical representation in `%s`.",
@@ -99,7 +99,7 @@ func InstallVersion(workingDir string, version util.SemVer) bool {
9999
log.Printf(
100100
"Failed to invoke `%s go version` in %s: %s\n",
101101
toolchainArg,
102-
versionCmd.Dir,
102+
workingDir,
103103
versionErr.Error(),
104104
)
105105

@@ -145,21 +145,20 @@ func SupportsWorkspaces() bool {
145145
}
146146

147147
// Constructs a `*exec.Cmd` for `go` with the specified arguments.
148-
func GoCommand(arg ...string) *exec.Cmd {
149-
cmd := exec.Command("go", arg...)
150-
registries.ApplyProxyEnvVars(cmd)
148+
func GoCommand(arg ...string) *registries.ProxiedCmd {
149+
cmd := registries.Command("go", arg...)
151150
return cmd
152151
}
153152

154153
// Run `go mod tidy -e` in the directory given by `path`.
155-
func TidyModule(path string) *exec.Cmd {
154+
func TidyModule(path string) *registries.ProxiedCmd {
156155
cmd := GoCommand("mod", "tidy", "-e")
157-
cmd.Dir = path
156+
cmd.SetDir(path)
158157
return cmd
159158
}
160159

161160
// Run `go mod init` in the directory given by `path`.
162-
func InitModule(path string) *exec.Cmd {
161+
func InitModule(path string) *registries.ProxiedCmd {
163162
moduleName := "codeql/auto-project"
164163

165164
if importpath := util.GetImportPath(); importpath != "" {
@@ -174,19 +173,19 @@ func InitModule(path string) *exec.Cmd {
174173
}
175174

176175
modInit := GoCommand("mod", "init", moduleName)
177-
modInit.Dir = path
176+
modInit.SetDir(path)
178177
return modInit
179178
}
180179

181180
// Constructs a command to run `go mod vendor -e` in the directory given by `path`.
182-
func VendorModule(path string) *exec.Cmd {
181+
func VendorModule(path string) *registries.ProxiedCmd {
183182
modVendor := GoCommand("mod", "vendor", "-e")
184-
modVendor.Dir = path
183+
modVendor.SetDir(path)
185184
return modVendor
186185
}
187186

188187
// Constructs a command to run `go version`.
189-
func Version() *exec.Cmd {
188+
func Version() *registries.ProxiedCmd {
190189
version := GoCommand("version")
191190
return version
192191
}
@@ -197,7 +196,7 @@ func RunList(format string, patterns []string, flags ...string) (string, error)
197196
}
198197

199198
// Constructs a `go list` command with `format`, `patterns`, and `flags` for the respective inputs.
200-
func List(format string, patterns []string, flags ...string) *exec.Cmd {
199+
func List(format string, patterns []string, flags ...string) *registries.ProxiedCmd {
201200
return ListWithEnv(format, patterns, nil, flags...)
202201
}
203202

@@ -220,11 +219,11 @@ func RunListWithEnv(format string, patterns []string, additionalEnv []string, fl
220219

221220
// Constructs a `go list` command with `format`, `patterns`, and `flags` for the respective inputs
222221
// and the extra environment variables given by `additionalEnv`.
223-
func ListWithEnv(format string, patterns []string, additionalEnv []string, flags ...string) *exec.Cmd {
222+
func ListWithEnv(format string, patterns []string, additionalEnv []string, flags ...string) *registries.ProxiedCmd {
224223
args := append([]string{"list", "-e", "-f", format}, flags...)
225224
args = append(args, patterns...)
226225
cmd := GoCommand(args...)
227-
cmd.Env = append(os.Environ(), additionalEnv...)
226+
cmd.AppendEnv(additionalEnv...)
228227
return cmd
229228
}
230229

0 commit comments

Comments
 (0)