Skip to content

Commit 6e050ed

Browse files
committed
fix: improve package logging
1 parent af428c7 commit 6e050ed

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

cli/internal/templating/engine.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,33 @@ import (
1414

1515
"github.com/jgfranco17/hackstack/cli/internal/fileutils"
1616
"github.com/jgfranco17/hackstack/cli/internal/logging"
17+
"github.com/sirupsen/logrus"
1718
)
1819

20+
const (
21+
extensionTemplate = ".j2"
22+
extensionRawCopy = ".copy"
23+
)
24+
25+
// Engine is the entity responsible for rendering embedded template
26+
// files with provided source data.
1927
type Engine struct {
2028
Files fs.FS
2129
Data CLIProject
2230
}
2331

32+
// NewEngine creates a new templating engine instance with the provided
33+
// embedded files and source data.
2434
func NewEngine(files fs.FS, data CLIProject) *Engine {
2535
return &Engine{
2636
Files: files,
2737
Data: data,
2838
}
2939
}
3040

41+
// Render processes the embedded template files and writes the output to the specified directory.
42+
// It walks through all files in the embedded FS, rendering templates and copying raw files as
43+
// needed. The function returns an error if any step of the rendering process fails.
3144
func (e *Engine) Render(ctx context.Context, outputPath string) error {
3245
logger := logging.FromContext(ctx).WithField("module", "templating")
3346

@@ -36,7 +49,7 @@ func (e *Engine) Render(ctx context.Context, outputPath string) error {
3649

3750
walker := func(path string, d fs.DirEntry, err error) error {
3851
if err != nil {
39-
return fmt.Errorf("walk error at %q: %w", path, err)
52+
return fmt.Errorf("walk error at %s: %w", path, err)
4053
}
4154
if d.IsDir() {
4255
return nil
@@ -46,16 +59,22 @@ func (e *Engine) Render(ctx context.Context, outputPath string) error {
4659

4760
var work func() error
4861
switch {
49-
case strings.HasSuffix(path, ".j2"):
50-
destPath = strings.TrimSuffix(destPath, ".j2")
62+
case strings.HasSuffix(path, extensionTemplate):
63+
destPath = strings.TrimSuffix(destPath, extensionTemplate)
5164
work = func() error {
52-
logger.WithField("file", path).Trace("Rendering from template")
65+
logger.WithFields(logrus.Fields{
66+
"source": path,
67+
"destination": destPath,
68+
}).Trace("Rendering from template")
5369
return renderTemplate(e.Files, path, destPath, e.Data)
5470
}
55-
case strings.HasSuffix(path, ".copy"):
56-
destPath = strings.TrimSuffix(destPath, ".copy")
71+
case strings.HasSuffix(path, extensionRawCopy):
72+
destPath = strings.TrimSuffix(destPath, extensionRawCopy)
5773
work = func() error {
58-
logger.WithField("file", path).Trace("Copying file")
74+
logger.WithFields(logrus.Fields{
75+
"source": path,
76+
"destination": destPath,
77+
}).Trace("Copying raw file")
5978
return fileutils.CopyFile(e.Files, path, destPath)
6079
}
6180
default:

0 commit comments

Comments
 (0)