@@ -7,9 +7,11 @@ import (
77 "os"
88 "path/filepath"
99 "strings"
10+ "sync/atomic"
1011 "text/template"
1112
12- "github.com/jgfranco17/hackstack/cli/internal/errorhandling"
13+ "golang.org/x/sync/errgroup"
14+
1315 "github.com/jgfranco17/hackstack/cli/internal/fileutils"
1416 "github.com/jgfranco17/hackstack/cli/internal/logging"
1517)
@@ -29,7 +31,9 @@ func NewEngine(files fs.FS, data CLIProject) *Engine {
2931func (e * Engine ) Render (ctx context.Context , outputPath string ) error {
3032 logger := logging .FromContext (ctx ).WithField ("module" , "templating" )
3133
32- count := 0
34+ var count atomic.Int64
35+ g , ctx := errgroup .WithContext (ctx )
36+
3337 walker := func (path string , d fs.DirEntry , err error ) error {
3438 if err != nil {
3539 return fmt .Errorf ("walk error at %q: %w" , path , err )
@@ -40,30 +44,38 @@ func (e *Engine) Render(ctx context.Context, outputPath string) error {
4044
4145 destPath := filepath .Join (outputPath , filepath .FromSlash (path ))
4246
47+ var work func () error
4348 switch {
4449 case strings .HasSuffix (path , ".j2" ):
4550 destPath = strings .TrimSuffix (destPath , ".j2" )
46- logger .WithField ("file" , path ).Trace ("Rendering from template" )
47- count ++
48- return renderTemplate (e .Files , path , destPath , e .Data )
51+ work = func () error {
52+ logger .WithField ("file" , path ).Trace ("Rendering from template" )
53+ return renderTemplate (e .Files , path , destPath , e .Data )
54+ }
4955 case strings .HasSuffix (path , ".copy" ):
5056 destPath = strings .TrimSuffix (destPath , ".copy" )
51- logger .WithField ("file" , path ).Trace ("Copying file" )
52- count ++
53- return fileutils .CopyFile (e .Files , path , destPath )
57+ work = func () error {
58+ logger .WithField ("file" , path ).Trace ("Copying file" )
59+ return fileutils .CopyFile (e .Files , path , destPath )
60+ }
5461 default :
55- return fmt .Errorf ("unrecognized resource extension for %q: expected .j2 or .copy " , path )
62+ return fmt .Errorf ("unrecognized resource extension for %q" , path )
5663 }
64+
65+ count .Add (1 )
66+ g .Go (work )
67+ return nil
5768 }
5869
5970 if err := fs .WalkDir (e .Files , "." , walker ); err != nil {
60- return & errorhandling.CommandError {
61- Err : fmt .Errorf ("failed to render templates: %w" , err ),
62- ExitCode : errorhandling .ExitTemplateError ,
63- HelpText : "Check template resources and verify the contents." ,
64- }
71+ return fmt .Errorf ("failed to render templates: %w" , err )
6572 }
66- logger .WithField ("fileCount" , count ).Debug ("Completed render" )
73+
74+ if err := g .Wait (); err != nil {
75+ return fmt .Errorf ("failed to render templates: %w" , err )
76+ }
77+
78+ logger .WithField ("fileCount" , count .Load ()).Debug ("Completed render" )
6779 return nil
6880}
6981
0 commit comments