diff --git a/cmd/cmd.go b/cmd/cmd.go index b5dbe13..925ed39 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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" @@ -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() }, }