Skip to content

Commit

Permalink
feat: concurrent processing
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmcgee committed Sep 10, 2024
1 parent c99e210 commit 6657fc7
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"golang.org/x/sync/errgroup"

"github.com/charmbracelet/log"
"github.com/numtide/godoc/pkg/markdown"
"github.com/numtide/godoc/pkg/parse"
Expand Down Expand Up @@ -37,20 +40,31 @@ var rootCmd = &cobra.Command{
sourceDir := args[0]
log.Infof("processing Go files in: %s", sourceDir)

return filepath.Walk(args[0], func(path string, info os.FileInfo, err error) error {
eg := errgroup.Group{}
eg.SetLimit(runtime.NumCPU())

if err := filepath.Walk(args[0], func(path string, info os.FileInfo, err error) error {
if !strings.HasSuffix(path, ".go") {
// skip
return nil
}

log.Infof("processing file: %s", path)
data, err := parse.File(path)
if err != nil {
return fmt.Errorf("failed to parse file: %w", err)
}
eg.Go(func() error {
log.Infof("processing file: %s", path)
data, err := parse.File(path)
if err != nil {
return fmt.Errorf("failed to parse file: %w", err)
}

return markdown.Write(outDir, data)
})
return nil
}); err != nil {
return fmt.Errorf("failed to walk source directory: %w", err)
}

return markdown.Write(outDir, data)
})
// wait for processing to complete
return eg.Wait()
},
}

Expand Down

0 comments on commit 6657fc7

Please sign in to comment.