Skip to content

Commit 6657fc7

Browse files
committed
feat: concurrent processing
1 parent c99e210 commit 6657fc7

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

cmd/cmd.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"runtime"
78
"strings"
89

10+
"golang.org/x/sync/errgroup"
11+
912
"github.com/charmbracelet/log"
1013
"github.com/numtide/godoc/pkg/markdown"
1114
"github.com/numtide/godoc/pkg/parse"
@@ -37,20 +40,31 @@ var rootCmd = &cobra.Command{
3740
sourceDir := args[0]
3841
log.Infof("processing Go files in: %s", sourceDir)
3942

40-
return filepath.Walk(args[0], func(path string, info os.FileInfo, err error) error {
43+
eg := errgroup.Group{}
44+
eg.SetLimit(runtime.NumCPU())
45+
46+
if err := filepath.Walk(args[0], func(path string, info os.FileInfo, err error) error {
4147
if !strings.HasSuffix(path, ".go") {
4248
// skip
4349
return nil
4450
}
4551

46-
log.Infof("processing file: %s", path)
47-
data, err := parse.File(path)
48-
if err != nil {
49-
return fmt.Errorf("failed to parse file: %w", err)
50-
}
52+
eg.Go(func() error {
53+
log.Infof("processing file: %s", path)
54+
data, err := parse.File(path)
55+
if err != nil {
56+
return fmt.Errorf("failed to parse file: %w", err)
57+
}
58+
59+
return markdown.Write(outDir, data)
60+
})
61+
return nil
62+
}); err != nil {
63+
return fmt.Errorf("failed to walk source directory: %w", err)
64+
}
5165

52-
return markdown.Write(outDir, data)
53-
})
66+
// wait for processing to complete
67+
return eg.Wait()
5468
},
5569
}
5670

0 commit comments

Comments
 (0)