func CopyGoFiles()

in tools/go-agent/tools/copy.go [32:75]


func CopyGoFiles(fromFS fs.ReadDirFS, fromDir, targetDir string,
	debugInfoBuilder func(entry fs.DirEntry, file *dst.File) (*DebugInfo, error),
	peek func(file *dst.File)) ([]string, error) {
	results := make([]string, 0)
	files, err := fromFS.ReadDir(fromDir)
	if err != nil {
		return nil, err
	}
	for _, f := range files {
		if !strings.HasSuffix(f.Name(), ".go") {
			continue
		}
		if strings.HasSuffix(f.Name(), "_test.go") || strings.HasSuffix(f.Name(), "_test_base.go") {
			continue
		}

		readFile, err := fs.ReadFile(fromFS, filepath.Join(fromDir, f.Name()))
		if err != nil {
			return nil, err
		}

		// ignore nocopy files
		if bytes.Contains(readFile, []byte(consts.DirecitveNoCopy)) {
			continue
		}

		parse, err := decorator.Parse(readFile)
		if err != nil {
			return nil, err
		}
		debugInfo, err := debugInfoBuilder(f, parse)
		if err != nil {
			return nil, err
		}

		peek(parse)
		copiedFilePath := filepath.Join(targetDir, f.Name())
		if err := WriteDSTFile(copiedFilePath, parse, debugInfo); err != nil {
			return nil, err
		}
		results = append(results, copiedFilePath)
	}
	return results, nil
}