MCPcopy Create free account
hub / github.com/bracesdev/errtrace / goListFiles

Function goListFiles

cmd/errtrace/main.go:284–339  ·  view source on GitHub ↗
(patterns []string)

Source from the content-addressed store, hash-verified

282var _execCommand = exec.Command
283
284func goListFiles(patterns []string) (files []string, err error) {
285 // The -e flag makes 'go list' include erroneous packages.
286 // This will even include packages that have all files excluded
287 // by build constraints if explicitly requested.
288 // (with "path/to/pkg" instead of "./...")
289 args := []string{"list", "-find", "-e", "-json"}
290 args = append(args, patterns...)
291
292 var stderr bytes.Buffer
293 cmd := _execCommand("go", args...)
294 cmd.Stderr = &stderr
295
296 stdout, err := cmd.StdoutPipe()
297 if err != nil {
298 return nil, errtrace.Wrap(fmt.Errorf("create stdout pipe: %w", err))
299 }
300
301 if err := cmd.Start(); err != nil {
302 return nil, errtrace.Wrap(fmt.Errorf("start command: %w", err))
303 }
304
305 type packageInfo struct {
306 Dir string
307 GoFiles []string
308 CgoFiles []string
309 TestGoFiles []string
310 XTestGoFiles []string
311 IgnoredGoFiles []string
312 }
313
314 decoder := json.NewDecoder(stdout)
315 for decoder.More() {
316 var pkg packageInfo
317 if err := decoder.Decode(&pkg); err != nil {
318 return nil, errtrace.Wrap(fmt.Errorf("output malformed: %w", err))
319 }
320
321 for _, pkgFiles := range [][]string{
322 pkg.GoFiles,
323 pkg.CgoFiles,
324 pkg.TestGoFiles,
325 pkg.XTestGoFiles,
326 pkg.IgnoredGoFiles,
327 } {
328 for _, f := range pkgFiles {
329 files = append(files, filepath.Join(pkg.Dir, f))
330 }
331 }
332 }
333
334 if err := cmd.Wait(); err != nil {
335 return nil, errtrace.Wrap(fmt.Errorf("%w\n%s", err, stderr.String()))
336 }
337
338 return files, nil
339}
340
341type fileRequest struct {

Callers 2

TestToolExecFunction · 0.85
expandPatternsFunction · 0.85

Calls 2

WrapFunction · 0.92
StringMethod · 0.65

Tested by 1

TestToolExecFunction · 0.68