Skip to content

Commit b9586a8

Browse files
committed
Go: Add functions for constructing go list commands
1 parent 6c0c336 commit b9586a8

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

go/extractor/toolchain/toolchain.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,14 @@ func RunList(format string, patterns []string, flags ...string) (string, error)
181181
return RunListWithEnv(format, patterns, nil, flags...)
182182
}
183183

184+
// Constructs a `go list` command with `format`, `patterns`, and `flags` for the respective inputs.
185+
func List(format string, patterns []string, flags ...string) *exec.Cmd {
186+
return ListWithEnv(format, patterns, nil, flags...)
187+
}
188+
184189
// Runs `go list`.
185190
func RunListWithEnv(format string, patterns []string, additionalEnv []string, flags ...string) (string, error) {
186-
args := append([]string{"list", "-e", "-f", format}, flags...)
187-
args = append(args, patterns...)
188-
cmd := exec.Command("go", args...)
189-
cmd.Env = append(os.Environ(), additionalEnv...)
191+
cmd := ListWithEnv(format, patterns, additionalEnv, flags...)
190192
out, err := cmd.Output()
191193

192194
if err != nil {
@@ -201,6 +203,16 @@ func RunListWithEnv(format string, patterns []string, additionalEnv []string, fl
201203
return strings.TrimSpace(string(out)), nil
202204
}
203205

206+
// Constructs a `go list` command with `format`, `patterns`, and `flags` for the respective inputs
207+
// and the extra environment variables given by `additionalEnv`.
208+
func ListWithEnv(format string, patterns []string, additionalEnv []string, flags ...string) *exec.Cmd {
209+
args := append([]string{"list", "-e", "-f", format}, flags...)
210+
args = append(args, patterns...)
211+
cmd := exec.Command("go", args...)
212+
cmd.Env = append(os.Environ(), additionalEnv...)
213+
return cmd
214+
}
215+
204216
// PkgInfo holds package directory and module directory (if any) for a package
205217
type PkgInfo struct {
206218
PkgDir string // the directory directly containing source code of this package

0 commit comments

Comments
 (0)